Reputation: 61
Hi I wish to convert this list of tuples into dictionary. As I am new to python, I am figuring out ways to convert into dictionary. I could only convert into dictionary if there is only one value. In my case, there are two values in it.I will demonstrate with more details below:
`List of tuples: [('Samsung', 'Handphone',10), ('Samsung', 'Handphone',-1),
('Samsung','Tablet',10),('Sony','Handphone',100)]`
As you can see above, I wish to identify 'Samsung' as the key and 'Handphone' and '10' as the values with respect to the key.
My desired output would be:
`Output: {'Sony': ['Handphone',100], 'Samsung': ['Tablet',10,'Handphone', 9]}`
As you can see above, the item 'handphone' and 'tablet' are group according to the key values which in my case is Sony and Samsung. The quantity of the item are are added / subtracted if they belong to the same item and same key (Samsung or Sony).
I would really appreciate any suggestions and ideas that you guys have in order to achieve the above output. I really ran out of ideas. Thank you.
Upvotes: 0
Views: 1236
Reputation: 311
Your output has a problem, that can be seen with proper identation:
{
'Sony': ['Handphone',100],
'Samsung': ['Tablet',10],
['Handphone', 9]
}
Handphone isn't a part of 'Samsung', you can do a list of lists to get:
{
'Sony': [
['Handphone',100]
],
'Samsung': [
['Tablet',10],
['Handphone', 9]
]
}
With:
my_list = [('Samsung', 'Handphone',10), ('Samsung', 'Handphone',-1), ('Samsung','Tablet',10),('Sony','Handphone',100)]
result = {}
for brand, device, value in my_list:
# first verify if key is present to add list for the first time
if not brand in result:
result[brand] = []
# then add new list to already existent list
result[brand] += [[device, value]]
But I think the best format would be a dict:
{
'Sony': {
'Handphone': 100
},
'Samsung': {
'Tablet': 10,
'Handphone': 9
}
}
That would be like:
my_list = [('Samsung', 'Handphone',10), ('Samsung', 'Handphone',-1), ('Samsung','Tablet',10),('Sony','Handphone',100)]
result = {}
for brand, device, value in my_list:
# first verify if key is present to add dict for the first time
if not brand in result:
result[brand] = {}
# then add new key/value to already existent dict
result[brand][device] = value
Upvotes: 0
Reputation: 1048
Good opportunity for defaultdict
from collections import defaultdict
the_list = [
('Samsung', 'Handphone', 10),
('Samsung', 'Handphone', -1),
('Samsung', 'Tablet', 10),
('Sony', 'Handphone', 100)
]
d = defaultdict(lambda: defaultdict(int))
for brand, thing, quantity in the_list:
d[brand][thing] += quantity
Result will be
{
'Samsung': {
'Handphone': 9,
'Tablet': 10
},
'Sony': {
'Handphone': 100
}
}
Upvotes: 3
Reputation: 37519
You can do this with a dictionary comprehension
What you really want are tuples for keys, which will be the company and the device.
tuples = [('Samsung', 'Handphone',10),
('Samsung', 'Handphone',-1),
('Samsung','Tablet',10),
('Sony','Handphone',100)]
d = {}
for company, device, n in tuples:
key = (company, device)
d[key] = d.get(key, 0) + n
Upvotes: 0