I need to convert the above list of tuples to nested dictionary without overwriting the value as below in python
[('a', '1'),
('b', 'true'),
('b', 'none'),
('a', '2'),
('b', 'true'),
('a', '3'),
('b', 'false')]
{'a': {'1' : { 'b' : ('true','none')},
'2' : { 'b' : ('true')},
'3' : { 'b' : ('false')}}}
Converting each tuple into dictionary using
dict()
and merging the dictionary doesn't work. Is there any pythonic way to do this?
Here's one way to do it with collections.defaultdict:
from collections import defaultdict
import pprint
data = [('a', '1'), ('b', 'true'), ('b', 'none'), ('a', '2'), ('b', 'true'), ('a', '3'), ('b', 'false')]
d = defaultdict(lambda: defaultdict(lambda: defaultdict(tuple)))
for i, j in data:
if i == 'a':
p = d[i][j]
else:
p[i] += j,
pprint.pprint(d)
# {'a': {'1': defaultdict(<class 'tuple'>, {'b': ('true', 'none')}),
# '2': defaultdict(<class 'tuple'>, {'b': ('true',)}),
# '3': defaultdict(<class 'tuple'>, {'b': ('false',)})}}
You could also use the dictionary's setdefault method to return default values for new keys, although the defaultdict approach is much cleaner and faster:
c = {}
for i, j in data:
if i == 'a':
q = c.setdefault(i, {}).setdefault(j, {})
else:
q[i] = q.setdefault(i, ()) + (j,)
pprint.pprint(c)
# {'a': {'1': {'b': ('true', 'none')},
# '2': {'b': ('true',)},
# '3': {'b': ('false',)}}}
Expanding #MosesKoledoye answer, if the first value in the dictionary is only 'a' and 'b', you know that the outer dictionary will always contain at most one element using 'a' as the key and the inner dictionary will always contain at most one element using 'b' as the key. So in the end you get the same information if it is {'1': ('true', 'none')…. You can convert that to your format simply by wrapping the data in some dictionaries. This means you can do the following
output = defaultdict(tuple)
for i, j in data:
if i == 'a':
current = j
else:
# i == 'b'
output[current] += (j, )
This will result in the following:
defaultdict(<type 'tuple'>, {'1': ('true', 'none'), '3': ('false',), '2': ('true',)})
Now to get it into a dictionary like yours you can do the following:
output = {k: {'b': v} for k, v in output.items()}
if output:
output = {'a': output}
Resulting in the following:
{'a': {'1': {'b': ('true', 'none')}, '3': {'b': ('false',)}, '2': {'b': ('true',)}}}
Related
list = [(a, 59.34), (t, 56.54), (d, 34.74), (u, 5.89)]
The list is descending order of the number(the second element)
I would like to format them like below.
a: 59.34
t: 56.54
d: 34.74
u: 5.89
details = []
for item in list:
name = item[0]
num = item[2]
details.append(f"{name}: {num:.2f}")
lst = [('a', 59.34), ('t', 56.54), ('d', 34.74), ('u', 5.89)]
details = [f"{x[0]}: {x[1]:.2f}" for x in lst]
print(*details, sep='\n')
Prints:
a: 59.34
t: 56.54
d: 34.74
u: 5.89
Looks like you want to convert the list to a dictionary. You can use dictionary comprehension to iterate through the list and convert the tuple(key, value) into key: value
list1 = [('a', 59.34), ('t', 56.54), ('d', 34.74), ('u', 5.89)]
dict_from_list = {key: val for (key, val) in list1}
print(dict_from_list)
# {'a': 59.34, 't': 56.54, 'd': 34.74, 'u': 5.89}
lst = [('a', 59.34), ('t', 56.54), ('d', 34.74), ('u', 5.89)]
list_comp = [f"{i[0]}: {i[-1]}" for i in lst]
print(list_comp)
(Ans)
['a: 59.34', 't: 56.54', 'd: 34.74', 'u: 5.89']
(Or )
for j in lst:
print(f"{j[0]}: {j[-1]}")
(Ans)
a: 59.34
t: 56.54
d: 34.74
u: 5.89
You could use a dictionary comprehension as your list already has elements as pairs therefore you have to just do the following:
items = [('a', 59.34), ('t', 56.54), ('d', 34.74), ('u', 5.89)] # try not to use the built-in method names or keywords as variable names
details = {k:v for k,v in items} # it uses the first element of the tuple as key and seocnd as a value
print(details)
Would output:
{'a': 59.34, 't': 56.54, 'd': 34.74, 'u': 5.89}
You could also use a loop:
items = [('a', 59.34), ('t', 56.54), ('d', 34.74), ('u', 5.89)]
details = {}
for i in items:
details[i[0]] = i[1]
print(details)
Would result in:
{'a': 59.34, 't': 56.54, 'd': 34.74, 'u': 5.89}
I got a list like this:
[['a','b','1','2']['c','d','3','4']]
and I want to convert this list to dictionary something looks like this:
{
('a','b'):('1','2'),
('c','d'):('3','4')
}
for example, ('a', 'b') & ('c','d') for key
and ('1','2') &('3','4') for value
so I used code something like this
new_dict = {}
for i, k in enumerate(li[0:2]):
new_dict[k] =[x1[i] for x1 in li[2:]]
print(new_dict)
,but it caused unhashable type error 'list'
I tried several other way, but it didn't work well..
Is there any way that I can fix it?
You can't have list as key, but tuple is possible. Also you don't need to slice on your list, but on the sublist.
You need the 2 first values sublist[:2] as key and the corresponding values is the sublist from index 2 sublist[2:]
new_dict = {}
for sublist in li:
new_dict[tuple(sublist[:2])] = tuple(sublist[2:])
print(new_dict) # {('a', 'b'): ('1', '2'), ('c', 'd'): ('3', '4')}
The same with dict comprehension
new_dict = {tuple(sublist[:2]): tuple(sublist[2:]) for sublist in li}
print(new_dict) # {('a', 'b'): ('1', '2'), ('c', 'd'): ('3', '4')}
li = [['a','b','1','2'],['c','d','3','4']]
new_dict = {}
for item in li:
new_dict[(item[0], item[1])] = (item[2], item[3])
I would use list-comprehension following way:
lst = [['a','b','1','2']['c','d','3','4']]
dct = dict([(tuple(i[:2]),tuple(i[2:])) for i in lst])
print(dct)
or alternatively dict-comprehension:
dct = {tuple(i[:2]):tuple(i[2:]) for i in lst}
Output:
{('a', 'b'): ('1', '2'), ('c', 'd'): ('3', '4')}
Note that list slicing produce lists, which are mutable and can not be used as dict keys, so I use tuple to convert these to immutable tuples.
You can do it with dict comprehension:
li = [
['a', 'b', '1', '2'],
['c', 'd', '3', '4'],
]
new_dict = {(i[0], i[1]): (i[2], i[3]) for i in li}
print(new_dict)
# result
{('a', 'b'): ('1', '2'), ('c', 'd'): ('3', '4')}
The scenario is that I have a 2-D list. Each item of the inner list is tuple (key, value pair). The key might repeat in the list. I want to create a default-dict on the fly, in such a way that finally, the dictionary stores the key, and the cumulative sum of all the values of that key from the 2-D list.
To put the code :
listOfItems = [[('a', 1), ('b', 3)], [('a', 6)], [('c', 0), ('d', 5), ('b', 2)]]
finalDict = defaultdict(int)
for eachItem in listOfItems:
for key, val in eachItem:
finalDict[key] += val
print(finalDict)
This is giving me what I want : defaultdict(<class 'int'>, {'a': 7, 'b': 5, 'c': 0, 'd': 5}) but I am looking for a more 'Pythonic' way using comprehensions. So I tried the below :
finalDict = defaultdict(int)
finalDict = {key : finalDict[key]+val for eachItem in listOfItems for key, val in eachItem}
print(finalDict)
But the output is : {'a': 6, 'b': 2, 'c': 0, 'd': 5} What is it that I am doing wrong? Or is it that when using comprehension the Dictionary is not created and modified on the fly?
Yes a comprehension can't be updated on-the-fly. Anyway, this task might be better suited to collections.Counter() with .update() calls:
>>> from collections import Counter
>>> c = Counter()
>>> for eachItem in listOfItems:
... c.update(dict(eachItem))
...
>>> c
Counter({'a': 7, 'b': 5, 'd': 5, 'c': 0})
This is because you do not assign any value to your finalDict inside your dict in comprehension.
In your dict in comprehension you are literally changing the type of finalDict
As far as I know you cannot assign value to your dict inside a dict in comprehension.
Here is a way to get the dictionnary you want
from functools import reduce
listOfItems = [[('a', 1), ('b', 3)], [('a', 6)], [('c', 0), ('d', 5), ('b', 2)]]
list_dict = [{key: val} for eachItem in listOfItems for key, val in eachItem]
def sum_dict(x, y):
return {k: x.get(k, 0) + y.get(k, 0) for k in set(x) | set(y)}
print(reduce(sum_dict, list_dict))
Simple solution without using additional modules:
inp_list = [[('a', 1), ('b', 3)], [('a', 6)], [('c', 0), ('d', 5), ('b', 2)]]
l = [item for sublist in inp_list for item in sublist] # flatten the list
sums = [(key, sum([b for (a,b) in l if a == key])) for key in dict(l)]
print(sums)
trying to use python's built-in methods instead of coding the functionality myself:
The long and explained solution
from itertools import chain, groupby
from operator import itemgetter
listOfItems = [[('a', 1), ('b', 3)], [('a', 6)], [('c', 0), ('d', 5), ('b', 2)]]
# just flat the list of lists into 1 list..
flatten_list = chain(*listOfItems)
# get all elements grouped by the key, e.g 'a', 'b' etc..
first = itemgetter(0)
groupedByKey = groupby(sorted(flatten_list, key=first), key=first))
#sum
summed_by_key = ((k, sum(item[1] for item in tups_to_sum)) for k, tups_to_sum in groupedByKey)
# create a dict
d = dict(summed_by_key)
print(d) # {'a': 7, 'b': 5, 'c': 0, 'd': 5}
~one line solution
from itertools import chain, groupby
from operator import itemgetter
first = itemgetter(0)
d = dict((k, sum(item[1] for item in tups_to_sum)) for k, tups_to_sum in groupby(sorted(chain(*listOfItems), key=first), key=first))
print(d) # {'a': 7, 'b': 5, 'c': 0, 'd': 5}
I have a list as a input made from tuples where the origin is the 1st object and the neighbour is the 2nd object of the tuple.
for example :
inp : lst = [('a','b'),('b','a'),('c',''),('a','c')]
out : {'a': ('a', ['b', 'c']), 'b': ('b', ['a']), 'c': ('c', [])}
first i tried to cast the list into a dictonary,
like this
dictonary = dict(lst)
but i got an error say that
dictionary update sequence element #0 has length 1; 2 is required
The simplest is probably inside a try / except block:
lst = [('a','b'),('b','a'),('c',''),('a','c')]
out = dict()
for k, v in lst:
try:
if v != '':
out[k][1].append(v)
else:
out[k][1].append([])
except KeyError:
if v != '':
out[k] = (k, [v])
else:
out[k] = (k, [])
print out
Which gives:
{'a': ('a', ['b', 'c']), 'b': ('b', ['a']), 'c': ('c', [])}
Here's how I did it, gets the result you want, you can blend the two operations into the same loop, make a function out of it etc, have fun! Written without Python one liners kung-fu for beginner friendliness!
>>> lst = [('a','b'),('b','a'),('c',''),('a','c')]
>>> out = {}
>>> for pair in lst:
... if pair[0] not in out:
... out[pair[0]] = (pair[0], [])
...
>>> out
{'a': ('a', []), 'c': ('c', []), 'b': ('b', [])}
>>> for pair in lst:
... out[pair[0]][1].append(pair[1])
...
>>> out
{'a': ('a', ['b', 'c']), 'c': ('c', ['']), 'b': ('b', ['a'])}
Just here to mention setdefault
lst = [('a','b'),('b','a'),('c',''),('a','c')]
d = {}
for first, second in lst:
tup = d.setdefault(first, (first, []))
if second and second not in tup[1]:
tup[1].append(second)
How can I convert the following list of tuples:
t = [("x", "1","11"),("x", "2","22"),("x", "3","33"),
("y", "3","00"),("z", "2","222"), ("z", "3","333")]
to this list of lists with dictionaries?
[["x",{"1":"11","2":"22","3":"33"}],
["y",{"3":"00"}],
["z",{"2":"222","3":"333"}]]
I'm a big fan of list comprehension. Here's a simple solution using it:
keys = set(map(lambda x: x[0], t))
d = [[k, dict([(y, z) for x, y, z in t if x is k])] for k in keys]
Result:
[['y', {'3': '00'}],
['x', {'1': '11', '2': '22', '3': '33'}],
['z', {'2': '222', '3': '333'}]]
This will be slow for larger lists as d is computed in O(N^2) time.
In two steps: create a dictionary and track the order you've seen the first elements, then build a list from that:
order = []
mapping = {}
for outer, inner, value in t:
if outer not in order:
order.append(outer)
mapping.setdefault(outer, {})[inner] = value
result = [(k, mapping[k]) for k in order]
or use a collections.OrderedDict() object to track the order you first saw the outer keys in:
from collections import OrderedDict
mapping = OrderedDict()
for outer, inner, value in t:
mapping.setdefault(outer, {})[inner] = value
result = mapping.items()
If order isn't important, use the first version and remove all references to order (3 lines), and just use mapping.items() at the end.
If your input is always sorted on the first element of each tuple, you can use itertools.groupby():
from itertools import groupby
from operator import itemgetter
result = [(k, {k: v for _, k, v in g}) for k, g in groupby(t, itemgetter(0))]
Demo:
>>> t = [("x", "1","11"),("x", "2","22"),("x", "3","33"),
... ("y", "3","00"),("z", "2","222"), ("z", "3","333")]
>>> order = []
>>> mapping = {}
>>> for outer, inner, value in t:
... if outer not in order:
... order.append(outer)
... mapping.setdefault(outer, {})[inner] = value
...
>>> [(k, mapping[k]) for k in order]
[('x', {'1': '11', '3': '33', '2': '22'}), ('y', {'3': '00'}), ('z', {'3': '333', '2': '222'})]
>>> mapping.items() # ignoring order
[('y', {'3': '00'}), ('x', {'1': '11', '3': '33', '2': '22'}), ('z', {'3': '333', '2': '222'})]
>>> from collections import OrderedDict
>>> mapping = OrderedDict()
>>> for outer, inner, value in t:
... mapping.setdefault(outer, {})[inner] = value
...
>>> mapping.items()
[('x', {'1': '11', '3': '33', '2': '22'}), ('y', {'3': '00'}), ('z', {'3': '333', '2': '222'})]
>>> from itertools import groupby
>>> from operator import itemgetter
>>> [(k, {k: v for _, k, v in g}) for k, g in groupby(t, itemgetter(0))]
[('x', {'1': '11', '3': '33', '2': '22'}), ('y', {'3': '00'}), ('z', {'3': '333', '2': '222'})]