Skip to content
PaulGoslawski edited this page Feb 5, 2020 · 2 revisions

Welcome to the HowToPython wiki!

Dictionary

Create a new dictionary with (class constructor):

d = dict(felix = (24,"fgia"), paul = (36,"fgia"))

or (literals):

d = {'felix': (24, 'fgia'), 'paul': (36, 'fgia')}

loop over dict with:

In [29]: for item in d.items():
    ...:     name = item[0]
    ...:     value = item[1]
    ...:     age = value[0]
    ...:     group = value[1]
    ...:     print(f"{name} is {age} years old and works in the {group} group!")
    ...:     
felix is 24 years old and works in the fgia group!
paul is 36 years old and works in the fgia group!

or use the more concise and pythonic syntax:

In [30]: for name, (age, group)in d.items():
    ...:     print(f"{name} is {age} years old and works in the {group} group!")
    ...:     
felix is 24 years old and works in the fgia group!
paul is 36 years old and works in the fgia group!

Clone this wiki locally