Reputation: 1646
I have a tuple with multiple dictionaries and I wish to unpack them for further use.
Let's say I have this:
tuple_to_add = {'name': 'custom1', 'value': 'first value'},{'name': 'custom2', 'value': 'second value'}
And I'd like to iterate through them I tried something like that but it didn't give me my answer:
for value, name in tuple_to_add:
print(value)
print(name)
Upvotes: 0
Views: 138
Reputation: 4043
Dictionaries can't be unpacked in a way tuples and lists can. If you had a tuple of tuples, your way of printing would work just fine:
tuple_to_add = ('custom1', 'first value'), ('custom2', 'second value')
for name, value in tuple_to_add:
# do whatever you want
You can simply iterate over the tuple and get the actual dictionary objects which you can use to get all the necessary information:
for dc in tuple_to_add:
print(dc['value'])
print(dc['name'])
Upvotes: 3