Created for
dictionary = {
'key1': value 1,
'key2': value 2,
'keyN': value N
}
Dictionary values can be retrieved from the collection using their respective keys.
item = dictionary_name[key]
prices = {
"apples": 2.50,
"oranges": 2.43,
"bananas": 3.50
}
apples_price = prices['apples']
print("{:.2f}".format(apples_price))
# 2.50
oranges_price = prices['oranges']
print("{:.2f}".format(oranges_price))
# 2.43
dictionary_name[key] = new_value
### change apples prices:
prices['apples'] = 2.20
print(prices)
# {'apples': 2.2, 'oranges': 2.43, 'bananas': 3.5}
dictionary_name[new_key] = new_value
prices = {
"apples": 2.50,
"oranges": 2.43,
"bananas": 3.50
}
### add new key:value pair:
prices['plums'] = 4.30
print(prices)
# {'apples': 2.5, 'oranges': 2.43, 'bananas': 3.5, 'plums': 4.3}
del
- operator
del dictionary[key]
prices = {
"apples": 2.50,
"oranges": 2.43,
"bananas": 3.50
}
### just delete 'oranges' key:value pair:
del prices['oranges']
print(prices)
# {'apples': 2.5, 'bananas': 3.5}
pop()
pop(key[, default])
pop()
- example
prices = {
"apples": 2.50,
"oranges": 2.43,
"bananas": 3.50
}
### remove 'apples' key:value pair from the dictinary, and return its value
apples_price = prices.pop('apples')
print(apples_price, prices)
# 2.5 {'oranges': 2.43, 'bananas': 3.5}
apples_price = prices.pop('apples', 5.00)
print(apples_price)
# 5.0
keys()
prices = {
"apples": 2.50,
"oranges": 2.43,
"bananas": 3.50
}
fruits = prices.keys()
print(fruits)
# dict_keys(['apples', 'oranges', 'bananas'])
values()
prices = {
"apples": 2.50,
"oranges": 2.43,
"bananas": 3.50
}
price_list = prices.values()
print(price_list)
# dict_values([2.5, 2.43, 3.5])
for key in dict_name:
# do something with a key
# do something with a value: dict_name[key]
prices = {
"apples": 2.50,
"oranges": 2.43,
"bananas": 3.50
}
for key in prices:
print("{} - {}".format(key, prices[key]))
# apples - 2.5
# oranges - 2.43
# bananas - 3.5
for key, value in dict_name.items():
# do something with a key
# do something with a value
prices = {
"apples": 2.50,
"oranges": 2.43,
"bananas": 3.50
}
for fruit, price in prices.items():
print("{} - {}".format(fruit, price))
# apples - 2.5
# oranges - 2.43
# bananas - 3.5
key:value
pairs.
set = {value1, value2, valueN}
int_numbers = {1, 2, 3, 4, 5}
print(int_numbers)
print(type(int_numbers))
# {1, 2, 3, 4, 5}
# <class 'set'>
int_dup_numbers = {1, 2, 3, 1, 2, 4, 3, 5, 1, 2, 3}
print(int_dup_numbers)
# {1, 2, 3, 4, 5, 6}
int_numbers = {1, 2, 3, 4, 5}
int_dup_numbers = {1, 2, 3, 1, 2, 4, 3, 5, 1, 2, 3}
print(int_numbers == int_dup_numbers)
# True
Returns new set, which elements are in either sets.
Pipe operator |
or method union
can be used
set1 = {1, 2, 3, 4}
set2 = {5, 4}
union1 = set1 | set2
union2 = set1.union(set2)
print(union1)
print(union2)
# {1, 2, 3, 4, 5}
# {1, 2, 3, 4, 5}
Returns new set, which elements belong to both sets.
Ampersand operator &
or method intersection
can be used
set1 = {1, 2, 3, 4}
set2 = {5, 4}
intersec1 = set1 & set2
intersec2 = set1.intersection(set2)
print(intersec1)
print(intersec2)
# {4}
# {4}
C = A - B, where C is a new set, which elements are the elements of A, which are not present in B
Operator -
or method difference
can be used
set1 = {1, 2, 3, 4, 5}
set2 = {5, 4}
dif1 = set1.difference(set2)
dif2 = set1 - set2
print(dif1)
print(dif2)
# {1, 2, 3}
# {1, 2, 3}
C = A △ B, where C is a new set, which elements are either in sets A or B but not in both.
Pipe operator |
or method union
can be used
set1 = {1, 2, 3, 4}
set2 = {5, 4}
sym_dif = set1.symmetric_difference(set2)
print(sym_dif)
# {1, 2, 3, 5}
Ivan
Maria
Georgy
These slides are based on
customised version of
framework