Various dictionary examples
examples/dictionary/person.py
person_1 = { 'fname': 'Moshe', 'lname': 'Cohen', 'email': 'moshe@cohen.com', 'children': ['Maya', 'Tal'], } person_2 = { 'fname': 'Dana', 'lname': 'Levy', 'email': 'dana@levy.com', 'phone': '123-456', }
examples/dictionary/people.py
from person import person_1, person_2 people = [person_1, person_2] print(people[0]['fname']) for person in people: print(person) print('----------------') people_by_name = { 'Moshe Cohen': 'moshe@cohen.com', 'Dana Levy': 'dana@levy.com', } print(people_by_name['Dana Levy']) for name, email in people_by_name.items(): print(f"{name} -> {email}") print('----------------') full_people_by_name = { 'Moshe': person_1, 'Dana': person_2, } print(full_people_by_name['Moshe']['lname']) print(full_people_by_name['Dana']) for fname, data in full_people_by_name.items(): print(fname) print(data)
Moshe {'fname': 'Moshe', 'lname': 'Cohen', 'email': 'moshe@cohen.com', 'children': ['Maya', 'Tal']} {'fname': 'Dana', 'lname': 'Levy', 'email': 'dana@levy.com', 'phone': '123-456'} ---------------- dana@levy.com Moshe Cohen -> moshe@cohen.com Dana Levy -> dana@levy.com ---------------- Cohen {'fname': 'Dana', 'lname': 'Levy', 'email': 'dana@levy.com', 'phone': '123-456'} Moshe {'fname': 'Moshe', 'lname': 'Cohen', 'email': 'moshe@cohen.com', 'children': ['Maya', 'Tal']} Dana {'fname': 'Dana', 'lname': 'Levy', 'email': 'dana@levy.com', 'phone': '123-456'}