Curs 3
Curs 3
Python 3.x
x = {1,2,3} #x = {1, 2, 3}
x.add(4) #x = {1, 2, 3, 4}
x.add(1) #x = {1, 2, 3, 4}
❖ Remove an element from the set ( methods remove or discard ). Remove throws an error if the set does not
contain that element. Use clear method to empty an entire set.
Python 3.x
x = {1,2,3} #x = {1, 2, 3} x = {1,2,3} #x = {1, 2, 3}
x.remove(1) #x = {2, 3} x.clear() #x = {}
x.discard(2) #x = {3}
x.discard(2) #x = {3}
SETS
Sets support a set of functions that can be used to modify its content. Some of these
functionalities can also be achieved by using some operators.
❖ Several elements can be added to a set by either use the member
function(method) update or by using the operator |=
Python 3.x
x = {1,2,3} #x = {1, 2, 3}
x |= {3,4,5} #x = {1, 2, 3, 4, 5}
x.update({5,6}) #x = {1, 2, 3, 4, 5, 6}
x.update({5,6},{6,7}) #x = {1, 2, 3, 4, 5, 6, 7}
x.update({8},{6},{9}) #x = {1, 2, 3, 4, 5, 6, 7, 8, 9}
❖ Total number of elements from a set can be found out using the len keyword
Python 3.x
x = {10,20,30,40}
y = len (x) #y = 4
SETS
Sets support a set of functions that can be used to modify its content. Some of these functionalities
can also be achieved by using some operators.
❖ Use method isdisjoint to test if a set has no common elements with another one
Python 3.x
x = {1,2,3,4}
y = {10,20,30,40}
z = x.isdisjoint(y) #z = True
❖ Use method issubset or operator <= to test if a set is included in another one
Python 3.x
x = {1,2,3,4}
y = {1,2,3,4,5,6}
z = x.issubset(y) #z = True
t = x <= y #t = True
SETS
Sets support a set of functions that can be used to modify its content. Some of these functionalities
can also be achieved by using some operators.
❖ Use method issuperset or operator >= to test if a set is included in another one
Python 3.x
x = {1,2,3,4}
y = {1,2,3,4,5,6}
z = y.issuperset(x) #z = True
t = y >= x #t = True
❖ Operator > can also be used → it checks if a set is included in another BUT is not identical to
it. Operator < can be used in the same way.
Python 3.x
x = {1,2,3,4} x = {1,2,3,4}
y = {1,2,3,4,5,6} y = {1,2,3,4}
t = y > x #t = True t = y > x #t = False
SETS
Sets support a set of functions that can be used to modify its content. Some of these
functionalities can also be achieved by using some operators.
❖ Use method pop to remove one element from the set. The remove element is
different from Python 2.x to Python 3.x in terms of the order the element are kept in
memory. Even if sets are unordered collection, in order to have quick access to
different elements of the set these elements must be kept in memory in a certain way.
Python 3.x
x = {"A","a","B","b",1,2,3}
Output (Python 3)
print (x)
print (x.pop()) {1, 2, 3, 'b', 'B', 'A', 'a'}
1
❖ The condition of the set (all elements are unique) still applies. In the next case, only the
first elements that meet the required criteria will be added.
Python 3.x
x = {i%5 for i in range(0,100)} #x = {0, 1, 2, 3, 4}
SETS AND BUILT-IN FUNCTIONS
The default build-in functions for list can also be used with sets and lambdas.
❖ Use map to create a new set where each element is obtained based on the
lambda expression provided.
Python 3.x
x = {1,2,3,4,5}
y = set(map(lambda element: element*element,x)) #y = {1,4,9,16,25}
x = [1,2,3]
y = [4,5,6]
z = set(map(lambda e1,e2: e1+e2,x,y)) #z = {5,7,9}
SETS AND BUILT-IN FUNCTIONS
The default build-in functions for list can also be used with sets and lambdas.
❖ Use filter to create a new set where each element is filtered based on the lambda expression
provided.
Python 3.x
x = [1,2,3,4,5]
y = set(filter(lambda element: element%2==0,x)) #y = {2,4}
❖ Both filter and map are used to create a set (usually in conjunction with range keyword)
Python 3.x
x = set(map(lambda x: x*x, range(1,10)))
#x = {1, 4, 9, 16, 25, 36, 49, 64, 81}
x = set(filter(lambda x: x%7==1,range(1,100)))
#x = {1, 8, 15, 22, 29, 36, 43, 50, 57, 64, 71, 78, 85, 92, 99}
SETS AND BUILT-IN FUNCTIONS
The default build-in functions for list can also be used with sets and lambdas.
❖ Other functions that work in a similar way as the build-in functions for list are min, max, sum, any,
all, sorted, reversed
❖ for statement can also be used to enumerate between elements of a set
Python 3.x
for i in {1,2,3,4,5}:
print(i)
❖ Python language also has another type → frozenset. A frozen set has all the characteristics of a
normal set, but it can not be modified. To create a frozen set use the frozenset keyword.
Python 3.x
x = frozenset ({1,2,3})
x.add(10) #!!!ERROR!!!
DICTIONARIES
A dictionary is python implementation of a hash-map container. Design as a (key – value pair) where Key
is a unique element within the dictionary.
A special keyword dict can be used to create a dictionary. The { and } can also be used to build a
dictionary – much like in the case of sets.
Python 3.x
x = dict() #x is an empty dictionary
x = {} #x is an empty dict (typeof(x)=“dict”)
x = {”A”:1, ”B”:2} #x is a dictionary with 2 keys
#(“A” and “B”)
x = dict(abc=1,aaa=2) #equivalent to x= {”abc”:1, ”aaa”:2}
x = dict({”abc”:1,”aaa”:2}) #equivalent to x= {”abc”:1, ”aaa”:2}
x = dict([(”abc”,1) ,(”aaa”,2)]) #equivalent to x= {”abc”:1, ”aaa”:2}
x = dict(((”abc”,1) ,(”aaa”,2))) #equivalent to x= {”abc”:1, ”aaa”:2}
x = dict(zip([”abc”,”aaa”],[1,2]))#equivalent to x= {”abc”:1, ”aaa”:2}
DICTIONARIES
To set a value in a dictionary use [] operator. The same operator can be used to read an existing
value. If a value does not exist, an exception will be thrown.
Python 3.x
x = {} #x is an empty dictionary
x[”ABC”] = 2 #x is a dictionary with one key (ABC)
y = x[”ABC”] #y = 2
y = x[”test”] #!!! ERROR !!!
To check if a key exists in a dictionary, use in operator; len can also be used to find out how may
keys a dictionary has.
Python 3.x
x = {”A”:1, ”B”:2} #x is a dictionary with 2 keys
”A” in x #True
len (x) #2
DICTIONARIES
Values from a dictionary can also be manipulated with setdefault member.
Python 3.x
x = {”A”:1, ”B”:2} #x = {”A”:1,”B”:2}
y = x.setdefault(”C”,3) #x = {”A”:1,”B”:2,”C:3”}, y=3
y = x.setdefault(”D”) #x = {”A”:1,”B”:2,”C:3”,”D”:None}, y=None
y = x.setdefault(”A”) #x = {”A”:1,”B”:2,”C:3”,”D”:None}, y=1
y = x.setdefault(”B”,20) #x = {”A”:1,”B”:2,”C:3”,”D”:None}, y=2
Method update can also be used to change the value associated with a key.
Python 3.x
x = {”A”:1, ”B”:2} #x = {”A”:1,”B”:2}
x.update({”A”:10}) #x = {”A”:10,”B”:2}
x.update({”A”:100,”B”:5}) #x = {”A”:100,”B”:5}
x.update({”C”:3}) #x = {”A”:100,”B”:5,”C”:3}
x.update(D=123,E=111) #x = {”A”:100,”B”:5,”C”:3,”D”:123,”E”:111}
DICTIONARIES
To delete an element from a dictionary use del keyword or clear method
Python 3.x
x = {”A”:1, ”B”:2} #x = {”A”:1,”B”:2}
del x[”A”] #x = {”B”:2}
x.clear() #x is an empty dictionary
del x[”C”] #!!! ERROR !!! “C” is not a key in x
To create a new dictionary you can use copy or static method fromkeys
Python 3.x
x = {”A”:1, ”B”:2} #x={”A”:1,”B”:2}
y = x.copy() #makes a shallow copy of x
y[”C”]=3 #x={”A”:1,”B”:2},y={”A”:1,”B”:2,”C”:3}
x = dict.fromkeys([”A”,”B”]) #x = {”A”:None,”B”:None}
x = dict.fromkeys([”A”,”B”],2)#x = {”A”:2,”B”:2}
DICTIONARIES
Elements from the dictionary can also be accessed with method get
Python 3.x
x = {”A”:1, ”B”:2} #x = {”A”:1,”B”:2}
y = x.get(”A”) #y = 1
y = x.get(”C”) #y = None
y = x.get(”C”,123) #y = 123
Output order may be different for different versions of python depending on how
data is stored/ordered in memory.
DICTIONARIES
All pairs from a dictionary can be obtained using the method items
Python 3.x
x = {”A”:1, ”B”:2} #x = {”A”:1,”B”:2}
y = x.items() #y = an iterable object (Python 3) or
#a list of tuples for Python 2.
#[ (”A”:1) , (”B”:2) ]