Reputation: 3707
I'm dealing with Python dicts now. I wrote a code:
import random
categories = {1 : "Antics", 2 : "Tickets", 3: "Moviez",
4 : "Music", 5 : "Photography", 6 : "Gamez", 7 : "Bookz",
8 : "Jewelry", 9 : "Computers", 10 : "Clothes"}
items = {"Picture" : 1, "Clock" : 1, "Ticket for Mettalica concert" : 2,
"Ticket for Iron Maiden concert" : 2, "Ticket for Placebo concert" : 2,
"The pianist" : 3, "Batman" : 3, "Spider-Man" : 3,
"WoW" : 6, "Cabal" : 6, "Diablo 3" : 6, "Diablo 2" : 6,
"Thinking in Java" : 7, "Thinking in C++" : 7, "Golden ring" : 8,
"Asus" : 10, "HP" : 10, "Shoes" : 11}
for key, val in categories :
for k, v in items :
if key == v :
print(val, k)
I wanted to create a 3rd dict, where i would have sth like :
dictThe3rd = {"Antics" : "Picture", "Antics" : "Clock", "Tickets" : "Ticket for Mettalica concert", "Ticket" : "Ticket for Iron Maiden concert", "Ticket" : "Ticket for Placebo concert", ...}
and so go on.
How to do this? My code shows:
test.py, line 14, in <module> for key, val in categories : TypeError: 'int' object is not iterable
Upvotes: 5
Views: 1223
Reputation: 54342
You should use items()
as in Stefan solution, but if you want to construct new dictionary instead of printing create list or append to it:
dictThe3rd = {}
for key, val in categories.items():
for k, v in items.items():
if key == v :
#print(val, k)
try:
dictThe3rd[val].append(k)
except KeyError:
dictThe3rd[val] = [k]
print(dictThe3rd)
Upvotes: 1
Reputation: 12960
Change your code to:
for key, val in categories.iteritems():
for k, v in items.iteritems():
if key == v:
print(val, k)
This way you can iterate over key/value pairs using an itemiterator which yields better performance than items()
for large dictionaries.
What you want is possible like this:
import collections
dictThe3rd=collections.defaultdict(set)
for key, val in categories.iteritems():
for k, v in items.iteritems():
if key == v:
dictThe3rd[val].add(k)
print dictThe3rd
which yields
{'Tickets': set(['Ticket for Mettalica concert', 'Ticket for Placebo concert',
'Ticket for Iron Maiden concert']),
'Jewelry': set(['Golden ring']),
'Antics': set(['Picture', 'Clock']),
'Moviez': set(['Batman', 'Spider-Man', 'The pianist']),
'Bookz': set(['Thinking in Java', 'Thinking in C++']),
'Gamez': set(['Diablo 3', 'Diablo 2', 'WoW', 'Cabal']),
'Clothes': set(['Asus', 'HP'])}
Upvotes: 2
Reputation: 2067
If you use a dict as an iterator, it will yield only its keys, not tuples of keys and values. You would have to use categories.items()
(or categories.iteritems()
if you are only using Python 2.x) to get those tuples:
for key, val in categories.items() :
for k, v in items.items() :
if key == v :
print(val, k)
You can use dict comprehensions to get a dict with category names as keys and all items in that category as values:
>>> dict3 = { catname : [ item for item, itemcat in items.items() if itemcat == cat ] for cat, catname in categories.items() }
>>> dict3
{'Antics': ['Picture', 'Clock'],
'Bookz': ['Thinking in C++', 'Thinking in Java'],
'Clothes': ['Asus', 'HP'],
'Computers': [],
'Gamez': ['Diablo 3', 'Diablo 2', 'Cabal', 'WoW'],
'Jewelry': ['Golden ring'],
'Moviez': ['The pianist', 'Batman', 'Spider-Man'],
'Music': [],
'Photography': [],
'Tickets': ['Ticket for Mettalica concert',
'Ticket for Placebo concert',
'Ticket for Iron Maiden concert']}
Explanation:
The inner comprehension in square brackets will list all items that have a category of cat
. The outer comprehension will iterate over all category keys and names, setting the category name as the new dict key and the inner comprehension as a value.
Upvotes: 6