Create your own game
eng   рус

Collections in Python

Previous tutorial: Loops and conditions in Python
Next tutorial: Functions in Python

In previous tutorial we've learnt following data types: int, float, complex, bool, str. In this one we'll talk about more complex types of Python.

In contrary to other programming languages Python has several built in data structures and has a great tools to work with them.

Arrays in Python - lists

List in Python is an ordered collection of elements. Each element of the list has it\'s own place. List can have several identical values. List is set by square brackets in which we place elements of the list:

a = [1,2,3] b = ['a','b','c'] c = [[1,2],2,'d'] d = [] # empty list e = [ {'a'}, (1,2), {'k':'v'}, [1,2] ]

We can store values with different types in one list. For example, last list contains set, tuple, dictionary and list. We'll talk about these data types soon.

Order of elements has a value, so next two lists are not equal:

a = [1,2,3] b = [2,1,3] bool( a == b) # False

Lists indexing

List's elements has indexes (order number). Indexes starts from zero: first list's element has index 0, second - 1... Indexes can be used to alter content of the element:

a = [1,2,3] a[0] = 323 # a = [323,2,3] a[2] = 32 # a = [323,2,32]

We can get access to the list from end. Then, negative indexes are used: -1 - last element of the list, -2 - last but one...

a = [323,2,32] a[-2] = 500 # a = [332,500,32] a[-1] = 0 # a = [332,500,0]

List slices

With slices we can get part of the list:

a = ['a','b','c','d','e','f'] b = a[1:3] # b = ['b','c']

In this case we've get slice of list a from element with index 1 (including it) to element with index 3 (without this element in new list). With slices we can get the beginning of the list and the ending::

a[:4] # first four elements of the list a[3:] # elements of the list from the fourth

Lists concatenation

Two lists can be joined with the concatenation. For this operation + sign is used:

a = [1,2,3] + [4,5,6] # a = [1,2,3,4,5,6]

Sets in Python

Set is unordered collection of elements. There are no two equal elements in the set. Non empty sets are created with curly braces. Empty set is created by the function set:

A = {1,2,3} B = {1,2,3,1,2} # {1,2,3} C = set() # empty set D = {1,"a",True} # set with different data types E = {(1,2),(3,4)} # set with tuples

Notice the last example. Set can not contain another collections of Python, except tuples (see below).

Mutating sets

For adding and removing elements of sets we can use functions add and remove:

a = {1,2,3} a.add(4) # a = {1,2,3,4} a.remove(1) # a = {2,3,4}

Function update allows unite two sets. For this we pass to update new set:

a = {1,2,3} a.update({2,3,4,5}) # a = {1,2,3,4,5}

Function intersection_update creates sets intersection:

a = {1,2,3} a.intersection_update({2,3,4,5}) # {2,3}

For intersection and union Python has another syntax:

{1,2,3} | {3,4} # sets union: {1,2,3,4} {1,2,3} & {3,4} # sets intersection: {3}

Tuples in Python

Tuple is ordered unmutable collection of elements. Word unmutable means that tuple once created couldn't be changed till the end of the program, i.e. we can add/delete elements of the tuple. This property allows to use tuples in sets.

Tuple is set in parentheses, and it's elements are separated by comma. Empty tuple is set by empty parentheses:

a = () # empty tuple b = (1,2) c = ( (1,2), (3,4), (5,6) ) d = { (1,2), (3,4), (5,6) )

You can use indexes and slices with tuples, same as in lists.


Dictionary is unordered collection of couples key - value. Dictionaries are set in curly braces. Empty dictionary - {}, that's why we use for empty set function set():

{} {'a': 1, 'b': 32} {1: 23, 2: 485} {1: "str1", 2: "str2"} {(1,2): 1, (0,1): "str2"}

Keys in dictionary are unique:

{ 0: 323, 0: 1}

This dictionary will contain only one element: {0:1}. Last pair will overwrite all previous.

Keys of the dictionaries can be used as indexes:

d = {0:'a','b': 'c'} d[0] d['a'] d[0] = 1

Python has functions for getting indexes, values and pairs (key, value) of dictionaries:

a = {0:'a',1:'b'} list(a.keys()) # keys [0,1] list(a.values()) # values ['a','b'] list(a.items()) # pairs (key, value) [(0,'a'),(1,'b')]

Notice that items return list of tuples where each tuple contain pair (key, value).

Changing type of collections

Python has functions that allows to change type of collection: list, tuple, set, dict. For example let's create some collections:

s = {1,2,3} l = [1,2,3] t = (1,2,3) d = {0:1,1:2,2:3}

Using function list on tuple and set will get us result [1,2,3]. But using list on collection d will return list [0,1,2], i.e. we will get list of keys. Same way functions tuple and set work.

Function dict works in another way. We can't just pass set, tuple or list, it's needed to use more complex collections:

dict([ (1,2), (3,4) ]) # { 1:2, 3:4 }

Remember that sets can contain tuples and not lists or dictionaries:

dict({ (1,2), (3,4) }) # that's ok dict({ [1,2], [3,4] }) # error!!

Lists and tuples: function reversed

Function reversed allows to get list (or tuple) in reversed order:

a = [1,2,3] list(reversed(a)) # [3,2,1] tuple(reversed(a)) # (3,2,1)

Notice that we explicitly write what collection we want to get. If you call reversed without pointing type of collection, you will get object of type reversed (if you passed tuple) or list_reverseiterator (if you passed list).

a = [1,2,3] type(reversed(a)) # <class 'list_reverseiterator'>

Element membership in collection

Keyword in allows to check if the element is a member of the collection:

1 in (1,2,3) # True 4 in {1,2,3} # False 3 in [1,2,3] # True 2 in {0:1,1:2} # False 0 in {0:1,1:2} # True

For dictionaries keys membership is checked.

Collections length - function len

Function len allows to know number of elements in any collection:

len([1,2,3]) # 3 len({1,2}) # 2

Function sum

sum function sums elements of collection. In dictionaries keys are summed:

sum([1,2,3]) # 6 sum({1,2}) # 3 sum({0:1,1:3}) # 1

If collection consist of tuples or lists another syntax is used for sum function:

sum([ [0,10], [3,2] ], []) # 10 sum({ (0,1), (2,3) }, ()) # 6

Assignment and copying of collections in Python

Let's look at the example:

a = {1,2,3} b = a b.remove(2) # b = {1,3}; a = {1,3}

Here we see that b and a are the same collection. While assignment Python doesn't create new collection, instead reference (another name) to collection is created. I.e. two variables are referenced to the same collection.

Python has copy function, which creates copy of existing collection:

a = {1,3} b = a.copy() b.add(3) # a = {1,3}, b = {1,2,3}

range function

Function range allows to get range of values and to create collection from these values. For example, range(10) call will create range from 0 to 9, in parentheses we write last value (not including it). To use range as a list we need to cast it:

list(range(3)) # [0,1,2]

We can point beginning value of the range, for example, range(2,5):

list(range(2,5)) # [2,3,4]


In this tutorial we've met built in data structures in Python - collections. In one of next tutorials we will discuss collections comprehensions which give great flexibility in work with collections.


No comments yet