i'm having issues exercise asking me loop through list of people , print know each person while using dictionary each person. i'm trying start out getting python accept , loop through dictionaries, whenever try run code, error message stating: "value error: many values unpack. (expected 2)"
dictionaries_v = { 'first_name': 'victor', 'last_name': 'croc', 'age': 21, 'city': 'new york', } dictionaries_c = { 'first_name': 'charmy', 'last_name': 'bee', 'age': 8, 'city': 'new york', } dictionaries_e = { 'first_name': 'espio', 'last_name': 'armadilo', 'age': 15, 'city': 'new york', } people = ['dictionaries_v', 'dictionaries_c', 'dictionaries_e'] key, value in people: print( "\n" + key + ": " + value)
thank time.
your first problem list holding 3 strings. notice how wrapped quotes around it. when iterate on list, not getting dictionary expect.
secondly, people
list
. so, when iterate list, iterator hold dictionary through each iteration.
knowing this, need iterate on list, other list:
people = [dictionaries_v, dictionaries_c, dictionaries_e] d in people: print(d)
your output like:
{'last_name': 'croc', 'age': 21, 'first_name': 'victor', 'city': 'new york'} {'last_name': 'bee', 'age': 8, 'first_name': 'charmy', 'city': 'new york'} {'last_name': 'armadilo', 'age': 15, 'first_name': 'espio', 'city': 'new york'}
to specific information dictionary, use key in each iteration. simple example:
for d in people: print(d['last_name'])
Comments
Post a Comment