Skip to content
English

Lists, dictionaries and other collections

A collection is a value that holds other values. Algorizm has five kinds.

Type Written as In short
ro'yxat (list) [1, 2, 3] ordered, can be changed
lug'at (dictionary) {"ism": "Ali"} values found by key, keeps the order of insertion
to'plam (set) {1, 2, 3} every value once, fast membership test
kortej (tuple) (1, 2) ordered, cannot be changed
baytlar (bytes) baytlar("Salom") raw bytes, cannot be changed

Lists

A list holds any number of values in order. Items are reached by their position, starting at 0; negative positions count from the end.

mevalar = ["olma", "anor"]
mevalar.qo'sh("uzum")
mevalar.joyla(0, "behi")
chiqar(mevalar, uzunlik(mevalar))

chiqar(mevalar[0], mevalar[-1], mevalar[1:3])
mevalar[1] = "nok"
mevalar.o'chir(0)
chiqar(mevalar, "nok" ichida mevalar)

sonlar = [5, 2, 9, 1]
sonlar.sarala()
chiqar(sonlar, yig'indi(sonlar), eng_katta(sonlar))
chiqar([1, 2] + [3], [0] * 4, sonlar.birlashtir("-"))
Output
["behi", "olma", "anor", "uzum"] 4
behi uzum ["olma", "anor"]
["nok", "anor", "uzum"] rost
[1, 2, 5, 9] 17 9
[1, 2, 3] [0, 0, 0, 0] 1-2-5-9
  • qo'sh(x) adds an item at the end, and joyla(i, x) inserts it at position i.
  • o'chir(i) removes and returns the item at position i; without an argument it removes the last one.
  • ichida tests whether a value is in the list.
  • + joins two lists into a new one, and * repeats a list.

A list is shared, not copied, when it is assigned to another name or passed to a function: both names then refer to the same list. Use nusxa() for an independent copy. All list methods are on the page Methods.

Slices

[a:b] takes the items from position a up to, but not including, b. Leaving out a means from the start, and leaving out b means to the end. Slices work the same way on lists, text, tuples and bytes, and always produce a new value.

r = [10, 20, 30, 40, 50]
chiqar(r[1:3], r[:2], r[3:], r[-2:])
chiqar("Algorizm"[2:5], (1, 2, 3)[1:])
Output
[20, 30] [10, 20] [40, 50] [40, 50]
gor (2, 3)

Dictionaries

A dictionary stores values under keys. A key is usually text or a number; it can be any value that cannot change, including a tuple. The items keep the order in which they were added.

talaba = {"ism": "Aziz", "yosh": 19}
talaba["kurs"] = 2
talaba["yosh"] += 1
chiqar(talaba["ism"], talaba, uzunlik(talaba))

chiqar("ism" ichida talaba, talaba.ol("manzil", "noma'lum"))
talaba.o'chir("kurs")

har kalit, qiymat ichida talaba.juftlar():
    chiqar(kalit, "=", qiymat)
chiqar(talaba.kalitlar(), talaba.qiymatlar())
Output
Aziz {"ism": "Aziz", "yosh": 20, "kurs": 2} 3
rost noma'lum
ism = Aziz
yosh = 20
["ism", "yosh"] ["Aziz", 20]
  • d[kalit] reads a value and is an error if the key is missing; d.ol(kalit, standart) returns the default instead.
  • d[kalit] = qiymat adds a key or replaces its value.
  • kalit ichida d tests whether a key exists.
  • har over a dictionary goes through its keys; juftlar() gives keys with values.
  • {} is an empty dictionary.

Sets

A set holds every value only once and has no positions. It is the right choice for removing duplicates and for asking "is this value among them?", which a set answers quickly however large it is.

a = {1, 2, 3, 2, 1}
b = to'plam([3, 4])
chiqar(a, uzunlik(a), 2 ichida a)
chiqar(a.birlashma(b), a.kesishma(b), a.ayirma(b))

a.qo'sh(10)
a.o'chir(1)
chiqar(saralangan(a))

harflar = to'plam("mississippi")
chiqar(uzunlik(harflar), to'plam(), tur({}))
Output
{1, 2, 3} 3 rost
{1, 2, 3, 4} {3} {1, 2}
[2, 3, 10]
4 to'plam() lug'at

The methods birlashma, kesishma and ayirma return the union, the intersection and the difference of two sets. Because {} is an empty dictionary, an empty set is written to'plam(). The members of a set must be values that cannot change, so lists, dictionaries and sets cannot be members.

Tuples

A tuple is a list that cannot be changed after it is made. It is written in round brackets; a tuple of one item needs a comma: (5,). Because it cannot change, a tuple may be a dictionary key or a member of a set.

nuqta = (3, 4)
x, y = nuqta
chiqar(nuqta, x, y, nuqta[0], uzunlik(nuqta))

a, b = 1, 2
a, b = b, a
chiqar(a, b)

funksiya chegaralar(r):
    qaytar eng_kichik(r), eng_katta(r)

kichik, katta = chegaralar([4, 9, 2])
chiqar(kichik, katta)

shaharlar = {(41, 69): "Toshkent", (41, 60): "Urganch"}
chiqar(shaharlar[(41, 60)], (5,), kortej([1, 2]))
Output
(3, 4) 3 4 3 2
2 1
2 9
Urganch (5,) (1, 2)

Tuples make it easy to handle several values at once:

  • x, y = nuqta takes a tuple or list apart into several variables.
  • a, b = b, a swaps two variables.
  • qaytar kichik, katta returns several values from a function as one tuple.

Bytes

baytlar holds raw bytes, numbers from 0 to 255. It is needed for files that are not text, such as images. Made from text, it holds the UTF-8 form of that text; the method matn() turns it back.

b = baytlar("Salom")
chiqar(b, uzunlik(b), b[0], b.o'n_oltilik())
chiqar(baytlar([72, 105]).matn(), baytlar("ў"), uzunlik(baytlar("ў")))
Output
baytlar("Salom") 5 83 53616c6f6d
Hi baytlar("ў") 2

Comparing collections

== compares collections by their contents, not by identity. Two dictionaries are equal when they hold the same keys with equal values, whatever the order. Lists, tuples and text can also be ordered with < and >: they are compared item by item, like words in a dictionary.

chiqar([1, 2] == [1, 2], {"a": 1, "b": 2} == {"b": 2, "a": 1})
chiqar((1, 5) < (2, 0), "anor" < "olma", [1, 2] != [2, 1])
Output
rost rost
rost rost rost

Sorting

The method sarala() sorts a list in place, and the function saralangan(r) returns a sorted copy of any sequence. Both accept a function that says what to sort by: it is called for each item, and the items are ordered by its results. If that function returns a tuple, the items are sorted by several fields at once.

odamlar = [{"ism": "Vali", "yosh": 30}, {"ism": "Ali", "yosh": 25}, {"ism": "Guli", "yosh": 28}]

yosh_boyicha = saralangan(odamlar, funksiya(o): o["yosh"])
chiqar(qo'lla(funksiya(o): o["ism"], yosh_boyicha))

odamlar.sarala(funksiya(o): o["ism"])
chiqar(qo'lla(funksiya(o): o["ism"], odamlar))
chiqar(eng_katta(odamlar, funksiya(o): o["yosh"])["ism"])
chiqar(saralangan(["nok", "anor", "uzum", "olma"], funksiya(s): (uzunlik(s), s)))
Output
["Ali", "Guli", "Vali"]
["Ali", "Guli", "Vali"]
Vali
["nok", "anor", "olma", "uzum"]

Sorting is stable: items that compare equal keep their original order. eng_katta and eng_kichik accept such a function in the same way.

Next

Functions shows how to give a name to a piece of a program and use it again.