loop through values from a list of dictionaries in Python - python

Given a list of dictionaries in python like this:
dict_list = [
{'a' : 1, 'b' : 2},
{'c' : 2, 'd' : 3},
{'x' : 4, 'y' : 5, 'z': 0}
]
What is the best to loop through all the values while emulating the obvious:
for i in dict_list:
for x in i.values():
print x
But ideally avoiding the nested for loops. I'm sure there must be a better way but I can't find it.

To loop through all the values, use itertools.chain.from_iterable
from itertools import chain
dict_list = [
{'a' : 1, 'b' : 2},
{'c' : 2, 'd' : 3},
{'x' : 4, 'y' : 5, 'z': 0}
]
for item in chain.from_iterable(i.values() for i in dict_list):
print item
Outputs:
1
2
2
3
5
4
0

Related

how to sort a Dictionary in list? [duplicate]

This question already has answers here:
How do I sort a list of dictionaries by a value of the dictionary?
(20 answers)
Closed 2 months ago.
I had Question in python
Imagine a list with dictionaries in it
how can we sort it by a value in dictionary ?
Imagine this list :
lst = [
{
"a" : 3,
"b" : 2
},
{
"a" : 1,
"b" : 4
},
{
"a" : 2,
"b" : 3
}
]
how can we sort this list by value of "a" in each dictionary (python)
i mean i want this list at the end :
lst = [
{
"a" : 1,
"b" : 4
},
{
"a" : 2,
"b" : 3
},
{
"a" : 3,
"b" : 2
}
]
You could provide a lambda key to sorted:
>>> lst = [
... {
... "a" : 3,
... "b" : 2
... },
... {
... "a" : 1,
... "b" : 4
... },
... {
... "a" : 2,
... "b" : 3
... }
... ]
>>> sorted(lst, key=lambda d: d["a"])
[{'a': 1, 'b': 4}, {'a': 2, 'b': 3}, {'a': 3, 'b': 2}]
One approach, use the key argument with itemgetter:
from operator import itemgetter
lst = [{"a": 3, "b": 2}, {"a": 1, "b": 4}, {"a": 2, "b": 3}]
res = sorted(lst, key=itemgetter("a"))
print(res)
Output
[{'a': 1, 'b': 4}, {'a': 2, 'b': 3}, {'a': 3, 'b': 2}]
From the documentation on itemgetter:
Return a callable object that fetches item from its operand using the
operand’s getitem() method. If multiple items are specified,
returns a tuple of lookup values. For example:
After f = itemgetter(2), the call f(r) returns r[2].
After g =
itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3]).

How to deduplicate dictionary in Python? Is there any method to override hashCode of some class like Java? [duplicate]

This question already has answers here:
List of unique dictionaries
(23 answers)
Closed 1 year ago.
elements = [
{'a' : 1, 'b' : 2, 'c': 3},
{'a' : 2, 'b' : 2, 'c': 3},
{'a' : 2, 'b' : 3, 'c': 3},
{'a' : 1, 'b' : 2, 'c': 3},
{'a' : 2, 'b' : 2, 'c': 3},
{'a' : 2, 'b' : 2},
{'a' : 1, 'b' : 2, 'c': 3, 'd' : 4},
{'v' : [1,2,3]}
]
Given above list of dict in Python, how to deduplicate to the following collection(order doesn't matter) efficiently
result = [
{'a' : 1, 'b' : 2, 'c': 3},
{'a' : 2, 'b' : 2, 'c': 3},
{'a' : 2, 'b' : 3, 'c': 3},
{'a' : 2, 'b' : 2},
{'a' : 1, 'b' : 2, 'c': 3, 'd' : 4},
{'v' : [1,2,3]}
]
The naive method is to use set, however dict in Python is unhashable. Right now, my solution is to serialize dict to String like json format (since dict has no order, two different strings can correspond to same dict. I have to keep some order). However this method has too high time complexity.
My Questions:
How to efficiently deduplicate dictionary in Python?
More generally, is there any method to override a class's hashCode like Java to use set or dict?
For your toy example with few data you can use the repr of the inner dictionaries as key for a new dictionary, then collect all the values:
elements = [{'a' : 1, 'b' : 2, 'c': 3}, {'a' : 2, 'b' : 2, 'c': 3},
{'a' : 2, 'b' : 3, 'c': 3}, {'a' : 1, 'b' : 2, 'c': 3},
{'a' : 2, 'b' : 2, 'c': 3}, {'a' : 2, 'b' : 2},
{'a' : 1, 'b' : 2, 'c': 3, 'd' : 4}, {'v' : [1,2,3]}]
kv = {repr(inner):inner for inner in elements}
elements = list(kv.values())
print(elements)
Output:
[{'a': 1, 'b': 2, 'c': 3}, {'a': 2, 'b': 2, 'c': 3}, {'a': 2, 'b': 3, 'c': 3},
{'a': 2, 'b': 2}, {'a': 1, 'b': 2, 'c': 3, 'd': 4}, {'v': [1, 2, 3]}]
If you check the id() of your inner dictionaries you'll see the last one survives.

Invert python dictionary from {key : [x, y, z]} to {x: key, y : key, z : key} [duplicate]

This question already has answers here:
Swap dictionary keys and values when values are lists
(1 answer)
Inverting a dictionary with list values
(6 answers)
Closed 2 years ago.
Sorry, if this one is duplicate, but I am new to python and haven't found solution by googling.
I have a dictionary that looks like:
{
1 : ['x', 'y'],
2 : ['z'],
3 : ['a']
}
All values in lists are guaranteed to be unique.
And I want to transform this dictionary to:
{
'x' : 1,
'y' : 1,
'z' : 2,
'a' : 3
}
What is the most pythonic and clean way to write this?
Try this:
dct = {
1 : ['x', 'y'],
2 : ['z'],
3 : ['a']
}
print({key:val for val, keys in dct.items() for key in keys})
Output:
{'a': 3, 'x': 1, 'y': 1, 'z': 2}
You can try this.
d = {
1 :['x', 'y'],
2 :['z'],
3 :['a']
}
'''
{
'x' : 1,
'y' : 1,
'z' : 2,
'a' : 3
}
'''
d2 = {}
for k,v in d.items():
for i in v:
d2[i] = k
print(d2)
Output:
{'x': 1, 'y': 1, 'z': 2, 'a': 3}
[Finished in 0.2s]

Python create dictionary from dataframe in loop

first post!
I am trying to create a function that create dictionary in loop from a dataframe.
Assume those 2 simplistic dataframes already exist:
data1 = {'A':[1, 2, 3, 4], 'B':[5, 6, 7, 8]}
df1 = pd.DataFrame(data)
dataframe1
and
data2 = {'C':[9, 10], 'D':[11, 12], 'E':[13, 14] }
df2 = pd.DataFrame(data2)
dataframe2
I want to be able to create a function like this:
def create_dict(df):
where the end results of df1 is:
dict1 = { 'A' : 1, 'B' : 5}
dict2 = { 'A' : 2, 'B' : 6}
dict3 = { 'A' : 3, 'B' : 7}
dict4 = { 'A' : 4, 'B' : 8}
and the end results of df2 is:
dict1 = { 'C' : 9, 'D' : 11, 'E' : 13}
dict2 = { 'C' : 10, 'D' : 12, 'E' : 14}
I was looking at dictionary comprehension to handle this, but I'm obviously not sure how to handle that problem. Thanks!
Use pandas.DataFrame.to_dict with records:
df1.to_dict(orient="records")
Output:
[{'A': 1, 'B': 5}, {'A': 2, 'B': 6}, {'A': 3, 'B': 7}, {'A': 4, 'B': 8}]

Unpack dictionary that contains a list of dictionaries and insert in columns

With the data below, I'm trying to unfold a dictionary that contains a list of dictionaries, and then group each key with the corresponding values of the other dictionaries together. For example:
result = {
'themes' : [{
'a' : 'orange',
'b' : 6,
'c' : 'neutral',
'd' : 6,
'e' : 0.24
}, {
'a' : 'banana',
'b' : 6,
'c' : 'neutral',
'd' : 6,
'e' : 0.16
}, {
'a' : 'phone',
'b' : 5,
'c' : 'neutral',
'd' : 5,
'e' : 0.02
}
]
}
...should become something along these lines:
themes={'a' : ['orange','banana', 'phone']}
count={'b' : [6,6,5]}
s_score={'c' : [neutral, neutral, neutral]}
...and so on.
I've looked here, here, and here among other places, but couldn't find something close enough to what I want to do. This came pretty close, but it's checking for at least one or more common values, whereas mine should group common keys. I know I can separate the outer key from the values like this:
>>>(k, v), = result.items()
>>>k
>>>'themes'
>>>v
>>>[{
'a' : 'orange',
'b :6,
'c' : 'neutral',
'd' : 6,
'e' : 0.24
}, {
'a' : 'banana',
'b' : 6,
'c' : 'neutral',
'd' : 6,
'e' : 0.16
}, {
'a' : 'phone',
'b' : 5,
'c' : 'neutral',
'd' : 5,
'e' : 0.02
}
]
but how do I get the v list of dictionaries to the way I described? Do I have to convert them to sets first?
To make my intention clear, my ultimate goal is iterate through the list of values of the keys that I want to keep, so I can enter them into their respective columns in my fairly basic flask-sqlalchemy SQLite database. So in the end I'll be able to query and get them displayed as html:
+-----------------+----------+----------+-------+
| a | b | c | d |
+-----------------+----------+----------+-------+
| orange | 2.4 | neutral | 6 |
| banana | 1.6 | neutral | 6 |
+-----------------+----------+----------+-------+
dict1 = {}
for eachKey in list(set(",".join(each.keys()) for each in result["themes"]))[0].split(","):
dict1[eachKey] = [each[eachKey] for each in result["themes"]]
print dict1
It will reduce your result to following dictionary-
{'a': ['orange', 'banana', 'phone'], 'c': ['neutral', 'neutral', 'neutral'], 'b': ['6', 6, 5], 'e': [0.24, 0.16, 0.02], 'd': [6, 6, 5]}
Try this using defaultdict
from collections import defaultdict
d = defaultdict(list)
for i,j in result.iteritems():
for k in j:
for l,m in k.iteritems():
d[l].append(m)
>>>d
defaultdict(<type 'list'>, {'a': ['orange', 'banana', 'phone'], 'c': ['neutral', 'neutral', 'neutral'], 'b': ['6', 6, 5], 'e': [0.24, 0.16, 0.02], 'd': [6, 6, 5]})
Now you can parse it by
themes = {'a':d['a']}
>>>themes
{'a': ['orange', 'banana', 'phone']}
And so on.Hope this helps
You can keep themes, count and score in one dictionary -- final_dict.
In code:
>>> lst = result['themes']
>>> final_dict = {}
>>> for d in lst:
... for (k, v) in d.items():
... final_dict.setdefault(k, []).append(v)
>>> print final_dict
{'a': ['orange', 'banana', 'phone'], 'c': ['neutral', 'neutral', 'neutral'], 'b': [6, 6, 5], 'e': [0.24, 0.16, 0.02], 'd': [6, 6, 5]}
You should first flatMap all your values in the list of tuples ([('a', 'orange'), ('c', 'neutral'), ('b', '6')..]) and then groupBy first element. I would do it this way:
import itertools
pairs = itertools.chain.from_iterable([d.items() for d in result["themes"]])
result = {}
for key, elem in pairs:
result.setdefault(key, []).append(elem)
print result

Categories