म
MarathiCodeby Sprout Tech
Documentation topicsLists
MarathiCode v0.2.0

Lists

यादी

A list is an ordered collection written in square brackets. Items can be of any kind, including other lists.

lists.mr
चल फळे = ["आंबा", "केळी", "पेरू"]
चल मिश्र = [1, "दोन", खरे, [3, 4]]
चल रिकामी = []
छापा(फळे)
छापा(मिश्र)
छापा(लांबी(रिकामी))
Output (आउटपुट)
["आंबा", "केळी", "पेरू"]
[1, "दोन", खरे, [3, 4]]
0

When a list is printed, strings inside it are shown in quotes.

Nested lists

nested.mr
चल x = [[1, 2], [3, 4]]
छापा(x[1][0])
छापा(लांबी(x))
छापा(लांबी(x[0]))
Output (आउटपुट)
3
2
2

Operations on lists

OperationResult
a + bA new list with the items of a followed by the items of b
a == bखरे when both have the same items in the same order
लांबी(a)Number of items
जोडा(a, item)Adds item to the end of a
a[i], a[i] = vRead or replace an item; see Indexing
list_ops.mr
चल a = [1, 2]
चल b = a + [3]
छापा(b)
छापा(a == [1, 2])
जोडा(a, 99)
छापा(a)
Output (आउटपुट)
[1, 2, 3]
खरे
[1, 2, 99]

Lists are shared

Assigning a list to another variable, or passing it to a function, does not copy it. Both names refer to the same list:

shared.mr
चल मूळ = [1, 2]
चल दुसरी = मूळ
दुसरी[0] = 100
छापा(मूळ)
Output (आउटपुट)
[100, 2]