One of the essential features of any programming language is the ability to store collection of data into a single variable. In Python there are 4 data types that allow to store collections of data and in this section we will focus on Sets while the section below will describe the Dictionaries.
A set is created by using curly brackets or the constructor set()
(this is also used to convert another data type to a set). It can be initialized empty or with already some values as shown in the example below.
s1={2,8,20,54} # Create a set with 4 elements print(s1) s2=set((5,2.8,'hi')) # Create a set with 3 elements print(s2)
A set has several properties:
s1={4,1.5,"hi"}
.
len()
as shown below
s1={1,2,3,4,5} print(len(s1)) #It prints 5
A set does not have an index, so it is not possible to access a single element of the set. It is however possible to use a for loop to iterate all the elements of the set.
s1 = {1,5.8,"Hello!"} for i in s1: print(i)
There are several built-in functions (available by simply adding a dot to the variable name, so set_name.function()
) that are associated to a Set and they can be useful for different types of operations. Here we mention some of the most relevant.
The function add()
is used to add a new element into a set.
s1={1,40,250} s1.add(36) # Include 36 into the set s1.add(40) # Cannot include 40 because is already in the set s1.add(58) # Include 58 into the set print(s1) # Prints {1,36,58,40,250}
The functions remove()
or discard()
are used to remove a new element from a set. The difference is that with the first function, if an element is not part of the set, an error is generated while the second function does not produce any error.
s1={1,40,250} s1.remove(40) # Removes 40 from the set s1.discard(40) # Tries to remove 40 but does not exist, so does nothing print(s1) # Prints {1,250}
The function update()
is used to merge two sets, by updating the set that calls the function. In case one set has some elements with the same values of the other set these are not included given that the final merged set should not have duplicates.
s1={1,2,3,4} s2={2,4,6,8} s3={1,2,3,4} s1.update(s2) print(s1) # Prints {1,2,3,4,6,8} s1.update(s3) print(s1) # Prints again {1,2,3,4,6,8}
It is also possible to merge two sets into a different set (i.e. without modifying the initial two sets). This is done with the function union()
:
s1={1,2,3,4} s2={2,4,6,8} s1.union(s2) print(s1) # Prints {1,2,3,4}, s1 did not change s3=s1.union(s2) print(s3) # Prints {1,2,3,4,6,8}
Together with union()
, there are other functions that are used in mathematics for operations between sets such as intersection()
which creates a new set that contains elements of both sets, or difference()
which creates a new set with only the element that are part of the set that calls the function but that are not present in the other set.
s1={1,2,3,4} s2={2,4,6,8} s3=s1.intersection(s2) print(s3) # Prints {2,4} s4=s1.difference(s2) print(s4) # Prints {1,3} s5=s2.difference(s1) print(s5) # Prints {6,8}
To copy a set, the function copy()
can be used and it creates a distinct copy of a set. By simply using the operator "=" the same set is associated to two different variables so a change to one of the variables affects also the other.
s1={1,2,3,4} s2=s1 # s2 is a reference to s1 s2.add(5) # Add 5 in s2 print(s1) # Prints {1,2,3,4,5}, 5 is added in s1 as well because they are the same s3=s1.copy() # s3 is a copy of s1 s3.add(6) # Add 6 in s2 print(s1) # s1 is still {1,2,3,4,5} print(s3) # Prints {1,2,3,4,5,6}
Try these operations and functions in the console below to gain familiarity with sets.
The Dictionaries are another of the 4 data types that allow the storage of collections of data. To create a Dictionary it is also required to use the curly brackets like the Sets, however here the data are stored in pair: one value is the key and the other value is the value. A different collection of data in which each element is another collection of data formed by a pair can be converted into a dictionary with dict()
. To check the data type the function is type()
.
d1={ "a":1, # "a" is the key and 1 is the value "b":2, "c":3 } print(d1) # Prints {'a': 1, 'b': 2, 'c': 3} l1=[[1,'hi'],[5,'hello']] # This is a list of lists print(type(l1)) # The type is 'list' d2=dict(l1) # Convert to dictionary print(type(d2)) # The type is not 'dict' print(d2) # Prints {1: 'hi', 5: 'hello'}
The properties of a dictionary are:
d1={ "A1":2, "A2":"hello", "B5":3.5 } print(len(d1)) #It prints 3
To check a specific value in the dictionary a key is required within square brackets.
d1={"a":1,"b":2} print(d1["a"]) # Prints 1 print(d1["b"]) # Prints 2
To change a value associated to a specific key it is used the key within square brackets and then this needs to be assigned to the new value. In the same way, to add a new key:value pair, the new key needs to be associated to the new values. Examples are shown below.
d1={"a":1,"b":2} print(d1) # Prints {'a':1,'b':2} d1["a"]=5 print(d1) # Prints {'a':5,'b':2} d1["c"]=10 print(d1) # Prints {'a':5,'b':2,'c':10}
Dictionaries have several built-in functions and here some of them are described. It is possible to know all the keys of a dictionary by using the function keys()
, all the values of a dictionary by using the function values()
and all the items of a dictionary (i.e. pairs of key:value) by using the function items()
. In all these three cases it is possible to convert the output to a list and access the different elements. In the case of the pairs key:value, the list is formed by tuple and each tuple has two elements, one is the key and the other is the value.
d1={"a":1,"b":2,"c":3} lkeys=list(d1.keys()) # List of keys print(lkeys) # Prints ['a', 'b', 'c'] lvalues=list(d1.values()) # List of values print(lvalues) # Prints [1, 2, 3] litems=list(d1.items()) # List of pairs key:value print(litems) # Prints [('a', 1), ('b', 2), ('c', 3)] print(type(litems[0])) # The element in this list is a tuple print(litems[0]) # Prints ('a', 1)
Another way of accessing a value of a dictionary, this time via function, is to use get()
and the key. Also, to add or change a value via function it is possible to use update()
and the key: if the key is not new, a specific value associated to that key is changed otherwise the new key:value item is added to the dictionary.
d1={"a":1,"b":2,"c":3} print(d1.get("c")) # Prints 3 d1.update({"b":4}) # Updates "b" print(d1) # Prints {'a':1,'b':4,'c':3} d1.update({"d":5}) # Adds "d" print(d1) # Prints {'a':1,'b':4,'c':3,'d':5}
To remove an element from the dictionary the function to use is pop()
and the key.
d1={"a":1,"b":2,"c":3} d1.pop("b") # Remove "b" print(d1) # Prints {'a':1,'c':3}
To copy a dictionary, like for the sets, the function copy()
can be used and it creates a distinct copy of the dictionary. By simply using the operator "=" the same dictionary is associated to two different variables so a change to one of the variables affects also the other.
d1={"a":1,"b":2,"c":3} d2=d1 # d2 is a reference to d1 d2.pop("c") # Remove "c" in d2 print(d1) # Prints {'a':1,'b':2}, "c" is removed in d1 as well because they are the same d3=d1.copy() # d3 is a copy of d1 d3.update({"d":8}) # Add "d":8 element in d3 print(d1) # d1 is still {'a':1,'b':2} print(d3) # Prints {'a':1,'b':2,'d':8}
Try these operations and functions in the console below to gain familiarity with dictionaries.