Dictionaries

--Originally published at Fundamentos de Programación

Dictionaries are just another data type such as lists or tuples. This dictionaries are indexed by keys, which can be any immutable type, or tuples if they don’t contain any mutable object.

mdgvx7r_700wa_0

You can create an empty dictionary with “{}”. You asociate key-value with “:”, separating each diferent key and value with a coma. It should look like this:

tel = {'jack': 4098, 'sape': 4139}

omj2y5m_700wa_0

The dict() function can create a dictionary pairing the key value types like this:

dict([(‘sape’, 4139), (‘guido’, 4127), (‘jack’, 4098)])

{‘sape’: 4139, ‘jack’: 4098, ‘guido’: 4127}

You can access any item of a dictionarie with it’s identifyer like this:

print(dict[‘identifier1’]) #Result: “Element 1”

wwlnamr_700wa_0

There are lot’s of functions you can attribute to dictionaries, some of them are str(), type(), dict.clear(), dict.copy(), dict.items(), dict.keys()… just to  mention a few.

Here is a very good tutorial:

Find out more at: https://docs.python.org/3/tutorial/datastructures.html

Follow me on Twitter: @danigguemez

#TC101 #Python #Dictionaries