Finding things in Python using dictionaries and lists
# Find people in dictionaries
# Working with lists and dictionaries
people list = [
{"name": "John", "age": 30, "occupation": "Engineer", "city": None },
{"name": "Alice", "age": 25, "occupation": "Teacher", "city": "Los Angeles"},
{"name": "Bob", "age": 35, "occupation": None, "city": "Chicago"},
{"name": "Eva", "age": 28, "occupation": "Artist", "city": "San Francisco"},
{"name": "David", "age": 40, "occupation": "Lawyer", "city": "Boston"},
{"name": "Sophie", "age": 22, "occupation": "Software Developer", "city": "Seattle"},
{"name": "Michael", "age": 45, "occupation": "Business Analyst", "city": "Dallas"},
{"name": "Olivia", "age": 32, "occupation": "Marketing Specialist", "city": "Miami"},
{"name": "Chris", "age": 38, "occupation": "Architect", "city": "Denver"},
{"name": "Emily", "age": 27, "occupation": "Graphic Designer", "city": "Portland"},
{"name": "Daniel", "age": 33, "occupation": "Chef", "city": "New Orleans"},
{"name": "Megan", "age": 29, "occupation": "Financial Analyst", "city": "Phoenix"},
{"name": "Ryan", "age": 36, "occupation": "Journalist", "city": "Atlanta"},
{"name": "Hannah", "age": 31, "occupation": "Event Planner", "city": "Las Vegas"},
]
while True:
search = input("What is the name of the person you are looking for? ").capitalize()
for person in people list:
if search == person["name"]:
# print("We found" , search + "!" , "AGE:" , person["age"], "," , "OCCUPATION:" , person["occupation"], ",", "CITY:" , person["city"],) # also able to print all in one line like this
print("We found", person["name"] + "!")
print("AGE:" , person["age"])
print("OCCUPATION:" , person["occupation"])
print("CITY:", person["city"])
break
else:
print(f"{search} not found in the list.")
continue
break # continue # find one person or repeat after finding
0 Comments