Dictionaries

Creating an empty dictionary

my_dict = dict()

or

my_dict = {}

Creating a dictionary with content

cupboard = {"shirts":3, "trousers":6, "tee-shirts":7}

=> {key:value, key:value...}

Adding an item to a dictionary

>>> my_dict = {}
>>> my_dict["login"] = "eric"
>>> my_dict["password"] = "azerty"
>>> my_dict
{'password': 'azerty', 'login': 'eric'}

Access a specific item

>>> my_dict["password"]
"azerty"

Delete an item

del keyword

>>> cupboard = {"shirts":3, "trousers":6, "tee-shirts":7}
>>> cupboard
{'shirts': 3, 'trousers': 6, 'tee-shirts': 7}
>>> del cupboard['shirts']
>>> cupboard
{'trousers': 6, 'tee-shirts': 7}

pop() method

Returns a value from the dictionary and removes the corresponding key

>>> cupboard = {"shirts":3, "trousers":6, "tee-shirts":7}
>>> cupboard
{'shirts': 3, 'trousers': 6, 'tee-shirts': 7}
>>> cupboard.pop('trousers')
6
>>> cupboard
{'shirts': 3, 'tee-shirts': 7}
>>> cupboard.pop('shirts')
3
>>> cupboard
{'tee-shirts': 7}
>>> cupboard.pop('tee-shirts')
7
>>> cupboard
{}

Dictionaries to store functions

def hello():
        print("Hello everyone !")

def bird():
        print("Chirp chirp !")

functions_dict = {}
functions_dict["hello"] = hello # Reference to function -> no parenthesis
functions_dict["bird"] = bird

functions_dict["hello"]()       # Returns "Hello everyone !"
functions_dict["bird"]()        # Returns "Chirp chirp !"

Iterate on keys

Note : dictionaries are not ordered, so the iteration might not return the keys/values entered the same way we entered them

Implicit

cupboard = {"shirts":3, "trousers":6, "tee-shirts":7}

for k in cupboard:
        print(k)

Returns :

shirts
trousers
tee-shirts

Explicit

Use keys() method to be more explicit (recommended ; exact same thing as the implicit one) :

cupboard = {"shirts":3, "trousers":6, "tee-shirts":7}

for k in cupboard:
        print(k)

Returns :

shirts
trousers
tee-shirts

Iterate on values

cupboard = {"shirts":3, "trousers":6, "tee-shirts":7}

for v in cupboard.values():
        print(v)

Returns :

3
6
7

Iterate on keys and values simultaneously

Works the same way as enumerate() for lists

cupboard = {"shirts":3, "trousers":6, "tee-shirts":7}

for k,v in cupboard.items():
        print("key :", k, "; value :", v)

Returns :

key : shirts ; value : 3
key : trousers ; value : 6
key : tee-shirts ; value : 7

Transform keys / values into a list of keys / values

dict.values() returns a dict_values object, and dict.keys() returns a dict_keys object. Those classes do not allow direct access to its values the list way list[i]. To do so we need to transform them into lists :

list(cupboard.keys())
list(cupboard.values())

Dictionaries and function parameters

Use the named parameters as a dictionary inside the function

We define a function that takes named parameters the following way :

def my_function(**named_params):
        ...

We call this function the following way :

my_function(param1=x, param2=y...)

We can access those parameters in the function as a dictionaru :

def my_function(**named_params):
        ...
        named_params['param1']
        named_params['param2']

Call a function with named parameters with a dictionary as parameter

Example with print() :

params = {"sep":" >> ", "end":" -\n"}
print("Here", "is", "an", "example", **params)

Returns :

Here >> is >> an >> example -

This is the same as calling the print() function the classic way :

print("Here", "is", "an", "example", sep=" >> ", end=" -\n")