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()
keys()
method of a dictionary returns a dict view object of dictionary keys, which means the view will reflect the changes of the dictionary keys.
prices = {
"apples": 2.50,
"oranges": 2.43,
"bananas": 3.50
}
fruits = prices.keys()
print(fruits)
# dict_keys(['apples', 'oranges', 'bananas'])
keys()
- dynamic view example
prices = {
"apples": 2.50,
"oranges": 2.43,
"bananas": 3.50
}
# get prices keys view:
prices_keys = prices.keys()
print("before:", prices_keys)
# add new key-value pair:
prices["new_key"]="new value"
# check if prices_keys contains the new key
print("after:", prices_keys)
before: dict_keys(['bananas', 'oranges', 'apples'])
after: dict_keys(['new_key', 'bananas', 'oranges', 'apples'])
values()
values()
method of a dictionary returns a dict view object dictionary 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])
values()
- dynamic view example
prices = {
"apples": 2.50,
"oranges": 2.43,
"bananas": 3.50
}
# get prices values view:
prices_values = prices.values()
print("before:", prices_values)
# change a value:
prices["oranges"]=100
# check if prices_values reflects the change:
print("after:", prices_values)
before: dict_values([3.5, 2.5, 2.43])
after: dict_values([3.5, 2.5, 100])
items()
items()
method of a dictionary returns a dict view object of dictionary items, i.e. a view of key-value pairs
prices = {
"apples": 2.50,
"oranges": 2.43,
"bananas": 3.50
}
prices_items = prices.items()
print(prices_items)
dict_items([('bananas', 3.5), ('apples', 2.5), ('oranges', 2.43)])
items()
- dynamic view example
prices = {
"apples": 2.50,
"oranges": 2.43,
"bananas": 3.50
}
# get prices items view:
prices_items = prices.items()
print("before:", prices_items)
# remove an item:
del prices["oranges"]
# check if prices_items reflects the change:
print("after:", prices_items)
before: dict_items([('apples', 2.5), ('oranges', 2.43), ('bananas', 3.5)])
after: dict_items([('apples', 2.5), ('bananas', 3.5)])
keys()
, values()
and items()
methods return view objects only in Python3. In Python2 they return lists.viewkeys()
, viewvalues()
and viewitems()
methods.
for key in dict_name:
# do something with a key
# this is equivalent to:
for key in dict_name.keys():
# do something with a key
prices = {
"apples": 2.50,
"oranges": 2.43,
"bananas": 3.50
}
for k in prices:
print(k)
bananas
apples
oranges
for value in dict_name.values():
# do something with a value
prices = {
"apples": 2.50,
"oranges": 2.43,
"bananas": 3.50
}
for v in prices.values():
print(v)
2.43
3.5
2.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
dict
class @python docskey: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'>
set_of_numbers = {1,2,3}
print(set_of_numbers) #{1, 2, 3}
set_of_strings = {"a", "b"}
print(set_of_strings) #{'b', 'a'}
set_of_imutables = {"a", 1, "b", 2, 3}
print(set_of_imutables) #{'a', 'b', 2, 3, 1}
set_of_mutables = { [1,2,3], ["a", "b"] }
print(set_of_imutables) #TypeError: unhashable type: 'list'
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}
student_scores
.student_scores
data, create a new data structure named best_students_scores
, storing the information (name and score) only for students with scores greater than 4.00best_students_scores
Ivan
Maria
Georgy
Maria - 5.5
Alex - 3.5
apple and banana one apple one banana
a red apple and a green apple
a - 2
green - 1
banana - 2
and - 2
one - 2
red - 1
apple - 4
str.split()
method, as shown:
text = "some words delimited by spaces"
words_list = text.split()
print(words_list)
['some', 'words', 'delimited', 'by', 'spaces']
|apple | 4 |
|banana| 2 |
|one | 2 |
|a | 2 |
|and | 2 |
|green | 1 |
|red | 1 |
id | name | number
---|---------|---------------
1 | "Maria" | "+39587111111"
2 | "Ivan" | "+39587222222"
3 | "Asen" | "+39587333333"
id | bill
---|---------
1 | 25.50
2 | 30.48
3 | 5.98
The user with highest bill - 30.48 is Ivan
The user with lowest bill - 5.98 is Asen
These slides are based on
customised version of
framework