Dictionaries in Python

Dictionaries in Python

Dictionaries can seem complicated subject, but when we look inside them, we will see not much:

>>> dir(dict)
[‘__class__’, ‘__contains__’, ‘__delattr__’, ‘__delitem__’, ‘__dir__’, ‘__doc__’
, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__gt__’,
 ‘__hash__’, ‘__init__’, ‘__iter__’, ‘__le__’, ‘__len__’, ‘__lt__’, ‘__ne__’, ‘_
_new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__setitem__’
, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘clear’, ‘copy’, ‘fromkeys’, ‘get
‘, ‘items’, ‘keys’, ‘pop’, ‘popitem’, ‘setdefault’, ‘update’, ‘values’]
>>>

There are some methods to look at.

>>> v
{‘Mirek’: 2, ‘Arek’: 1, ‘Romek’: 4, ‘Adam’: 3}
>>> w
{‘Mirek’: 2, ‘Arek’: 1, ‘Romek’: 4, ‘Adam’: 3}

dict1 = dict,copy()

>>> x = v.copy()
>>> x
{‘Mirek’: 2, ‘Adam’: 3, ‘Romek’: 4, ‘Arek’: 1}

We can copy our dictionary to a variable that will be another dictionary with the same items.

dict.clear()

>>> v.clear()
>>> v
{}
>>>

We can clear our dictionary. It exists but has no items.

dict.update(dic1)

>>> c = {“Hela”: “a”, “Aniela”: “b”, “Ala”: “c”}
>>> c
{‘Aniela’: ‘b’, ‘Hela’: ‘a’, ‘Ala’: ‘c’}
>>> w.update(c)

>>> w
{‘Mirek’: 2, ‘Hela’: ‘a’, ‘Romek’: 4, ‘Ala’: ‘c’, ‘Aniela’: ‘b’, ‘Adam’: 3, ‘Are
k’: 1}
>>>

We can update our dictionary using another dictionary.

 dict.pop(key)

>>> w
{‘Mirek’: 2, ‘Hela’: ‘a’, ‘Romek’: 4, ‘Ala’: ‘c’, ‘Aniela’: ‘b’, ‘Adam’: 3, ‘Are
k’: 1}

>>> w.keys()
dict_keys([‘Mirek’, ‘Hela’, ‘Romek’, ‘Ala’, ‘Aniela’, ‘Adam’, ‘Arek’])
>>>

>>> w.pop(‘Aniela’)
‘b’

>>> w
{‘Mirek’: 2, ‘Hela’: ‘a’, ‘Romek’: 4, ‘Ala’: ‘c’, ‘Adam’: 3, ‘Arek’: 1}
>>>

We can read and delete a value from our dictionary. We can choose which item we need to delete — the key should be insert into the parentheses.

 dict.popitem()

>>> w
{‘Mirek’: 2, ‘Hela’: ‘a’, ‘Romek’: 4, ‘Ala’: ‘c’, ‘Aniela’: ‘b’, ‘Adam’: 3, ‘Are
k’: 1}
>>> w.popitem()
(‘Mirek’, 2)
>>> w.popitem()
(‘Hela’, ‘a’)
>>> w.popitem()
(‘Romek’, 4)
>>>

We can read and delete a value from our dictionary. It takes no arguments in its parentheses. It deletes from the beginning of the dictionary.

dict.get()

>>> w.get(‘Adam’)
3
>>> w.get(3)
>>> w.get(‘Aleksander’)
>>>

We can return a value for the given key. If key is not available in our dictionary, then it returns default value None.

dict.keys()

>>> w.keys()
dict_keys([‘Ala’, ‘Aniela’, ‘Adam’, ‘Arek’])
>>>

We can get all keys in our dictionary.

dict.values()

>>> w.values()
dict_values([‘c’, ‘b’, 3, 1])
>>>

We can get all values in our dictionary.

dict.items()

>>> w.items()
dict_items([(‘Ala’, ‘c’), (‘Aniela’, ‘b’), (‘Adam’, 3), (‘Arek’, 1)])
>>>

The method items() returns a list of tuple pairs (key, value) for our dictionary.

 dict.fromkeys(seq[,value]) -> New dict

>>> r = (1,2,3)
>>> w.fromkeys(r)
{1: None, 2: None, 3: None}
>>>

>>> w.fromkeys(r, 120)
{1: 120, 2: 120, 3: 120}
>>>

>>> w.fromkeys(r, ‘abc’)
{1: ‘abc’, 2: ‘abc’, 3: ‘abc’}
>>>

We have a sequence, and we insert it into the method. Value defaults to None, so we have None assigned to all items in our sequence. However, we can set another value, for example 120. This integer will be insert as a value to the items of the sequence.

dict.setdefault(key[,value]) -> dict

>>> w.setdefault(‘Aniela’)
‘b’
>>> w.setdefault(‘Adam’)
3
>>> w.setdefault(‘Aniela’, None)
‘b’
>>> w.setdefault(‘Adam’, None)
3
>>> w.setdefault(‘Krzysztof’)
>>> w.setdefault(‘Krzysztof’, None)
>>> w.setdefault(‘Agnieszka’, 5)
5
>>>

This method returns values, when we insert keys. If the method doesn’t find a key, it will return None by default. Or we can set other value that will be returned by default.

Leave a comment