Dictionaries and Sets - ch09
Dictionaries and Sets - ch09
and Sets
Dictionaries
• We can specify the value of elements in lists and tuples. We may want to
define both the key and the value in some cases.
Now let's define a dictionary that will hold English-Turkish word pairs.
lexicon = {"car": "araba", "image": "resim", "coding": "kodlama", "software":
"yazılım"} # In this dictionary, the English of the word is the key, and the Turkish is the
value.
# To access any value in the dictionary, the key of that word (value) must be used.
word = lexicon["software"]
print(word)
yazılım
Accessing Dictionary Elements
If you try to reach the value of a key that is not in the dictionary, a KeyError error occurs.
The get() command allows us to find the value of the key written in parentheses.
However, if the key does not exist, it does not generate a KeyError, it produces a None
result.
None
Adding and Editing Elements to
the Dictionary
dictionary_variable["key"] = "value"
lexicon["pink"] = "pembe"
print(lexicon)
The clear() command is used to delete all the elements of the dictionary. After the clear()
command is executed, all the elements in the dictionary are deleted and the empty dictionary {} is
printed to the screen.
lexicon.clear()
print(lexicon)
{}
Listing Dictionary Elements
The elements in the dictionary can be listed with the help of the for loop.
The items() command allows us to access both keys and values simultaneously in a dictionary.
We can use the items() command with the for loop as follows.
lexicon = {"car": "araba", "image": "resim", "coding": "kodlama", "software": "yazılım"}
for ky, val in lexicon.items():
print("Key:", ky, "Value:", val)
The keys() and values() commands are used to separately retrieve the keys and values of the
dictionary.
The values in the dictionary can be retrieved with the values() command.
araba
resim
kodlama
yazılım
len()
The in and not in commands are used to check if a key exists in the dictionary.
The point to be noted here is that only a key can be checked for existence in the
dictionary, while the existence of the value cannot be checked.
True
True
Equality of Dictionaries
The equality of two dictionaries is checked with the == operator, and the
unequal condition is checked with the != operator.
Two dictionaries being equal means that the keys and their corresponding
values, that is, the key-value pairs, are equal.
Even one of the key-value pairs in the dictionary is different makes the two
dictionaries different.
Equality of Dictionaries
True
update()
The clear() command deletes all the elements in the dictionary, but the
dictionary structure itself is not deleted, the dictionary becomes empty {}
wind = {"north": "north wind", "south": "south wind", "east": "east wind"}
wind.clear()
print(wind)
{}
Dictionary Variables
In other words, the address information where the data is kept in the
memory is held in the dictionary type variables.
140723374014016
140723374014016
Dictionary Variables
We see that the above operation does not copy the contents of a dictionary,
only the values of the variables are synchronized, there is still a single
dictionary dataset in memory.
copy()
In the following case, the memory addresses of the two variables are not
synchronized, a second copy of the values is created, in this case, changes
to any variable will not affect the other.
wind = {"north": "north wind", "south": "south wind", "east": "east wind"}
predict = wind.copy()
wind["northwest"] = "nw wind"
print(predict)
Sets
set1 = set()
Sets
lotr = set(list1)
lotr.add("9")
print(lotr)
Sets
set3 = {4, 8, 9}
set3.remove(8)
print(set3)
• Although the discard() method does the same job as remove(), the
only difference is that if an element that is not in the set is to be
deleted, an error will not occur.
set4 = {4, 8, 9}
Sets
• Instead of finding the difference between two sets using the difference() method,
we can also find the difference with '-' operator directly.
• We can find the intersection of set1 and set2 using the intersection() method or the '&'
operator.
• The isdisjoint() method is used to determine the disjoint status of two sets, that is, whether
their intersection is empty or not. This method produces True if the sets are discrete, False
otherwise.
set1 = {4, 9, 6}
set2 = {3, 6, 5}
disjSets = set1.isdisjoint(set2)
print(disjSets)
• The issubset() method is used to check whether a set is a subset of another set.
set1 = {4, 9, 6}
set2 = {6, 4, 7, 9, 5}
print(set1.issubset(set2)) # where set1 and set2 are used is important here
• As opposed to subset querying, issuperset() method is used to find if a set is superset of another set or
Sets