I have list of dictionary
dictio =[{'key1':'value1'}, {'key1':'value2'}, {'key1':'value1'}, {'key2':'value4'}, {'key2':'value5'}]
from collections import defaultdict
result = defaultdict(list)
for subd in dictio:
for k, v in subd.items():
result[k].append(v)
result
My output
While appending 'value1' appending to 'key1' which is not required
defaultdict(list,
{'key1': ['value1', 'value2', 'value1'],
'key2': ['value4', 'value5']})
My Expected
`defaultdict(list,
{'key1': ['value1', 'value2'],
'key2': ['value4', 'value5']})`
You can turn your list of dict into a set and then in a list again, a set would remove all duplicates.
dictio = list(set([{'key1':'value1'}, {'key1':'value2'}, {'key1':'value1'}, {'key2':'value4'}, {'key2':'value5'}]))
You can achieve that very simply, just check if the element is already in:
dictio =[{'key1':'value1'}, {'key1':'value2'}, {'key1':'value1'}, {'key2':'value4'}, {'key2':'value5'}]
from collections import defaultdict
result = defaultdict(list)
for subd in dictio:
for k, v in subd.items():
if v not in result[k]:
result[k].append(v)
Instead of list use set
Ex:
dictio =[{'key1':'value1'}, {'key1':'value2'}, {'key1':'value1'}, {'key2':'value4'}, {'key2':'value5'}]
from collections import defaultdict
result = defaultdict(set)
for subd in dictio:
for k, v in subd.items():
result[k].add(v)
result
# --> defaultdict(<class 'set'>, {'key1': {'value2', 'value1'}, 'key2': {'value4', 'value5'}})
Try this example from https://www.w3schools.com/python/python_howto_remove_duplicates.asp
mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)
Related
Very similar to this question but with an added caveat.
I have two lists identical in length. One contains my keys, and the other contains a list of dictionaries belonging to said keys. Example below (yes the Nones are intentional)
keys = ['key1', 'key2, 'key3']
vals = [[{'subkey1': 'val1', 'subkey2': 'val2'}], [{'subkey1: 'val2'},None],[{'subkey1':'val3'}, {'subkey3':'val1}, None, None]]
What I'd like to do is match each dictionary in each list to a key to create a nested dict.
So something like below:
final = {'key1':{'subkey1': 'val1', 'subkey2': 'val2'}, 'key2':{'subkey1': 'val2'}, 'key3':{'subkey1':'val3'}, {'subkey3':'val1'}}
I know we can use dictionary comprehension to do this. I created a dictionary of empty dicts.
s = {}
for i in keys:
for v in vals:
s[i] = {}
break
Then I wrote the following to iterate through the list of dicts I'd like to pair up accounting for the non dictionary values in my list.
x = 0
while x < len(keys):
for i in keys:
for element in vals[x]:
if isinstance(element, dict) == True:
s[str(i)].append(element)
else:
print('no')
x=x+1
However when I run this, I get AttributeError: 'NoneType' object has no attribute 'append'
How can I append to a dictionary using this for loop?
For the less readable dictionary comprehension solution
keys = ['key1', 'key2', 'key3']
vals = [
[{'subkey1': 'val1', 'subkey2': 'val2'}],
[{'subkey1': 'val2'}, None],
[{'subkey1': 'val3'}, {'subkey3':'val1'}, None, None]
]
s = {
k: {
sk: sv for d in (x for x in v if x is not None) for sk, sv in d.items()
} for k, v in zip(keys, vals)
}
Gives
{'key1': {'subkey1': 'val1', 'subkey2': 'val2'},
'key2': {'subkey1': 'val2'},
'key3': {'subkey1': 'val3', 'subkey3': 'val1'}}
This can be done fairly easily using a defaultdict:
from collections import defaultdict
keys = ['key1', 'key2', 'key3']
vals = [
[{'subkey1': 'val1', 'subkey2': 'val2'}],
[{'subkey1': 'val2'},None],
[{'subkey1':'val3'}, {'subkey3':'val1'}, None, None]
]
final = defaultdict(dict)
for idx, key in enumerate(keys):
for d in vals[idx]:
if d is not None:
final[key].update(d)
Output:
defaultdict(<class 'dict'>, {
'key1': {'subkey1': 'val1', 'subkey2': 'val2'},
'key2': {'subkey1': 'val2'},
'key3': {'subkey1': 'val3', 'subkey3': 'val1'}
})
can't use append on a dict
try this maybe?
keys = ['key1', 'key2', 'key3']
vals = [[{'subkey1': 'val1', 'subkey2': 'val2'}], [{'subkey1': 'val2'},None],[{'subkey1':'val3'}, {'subkey3':'val1'}, None, None]]
s = {}
for i in keys:
s[i] = {}
for i, key in enumerate(keys):
for element in vals[i]:
if isinstance(element, dict) == True:
s[str(key)].update(element)
print(s)
Output:
{'key1': {'subkey1': 'val1', 'subkey2': 'val2'}, 'key2': {'subkey1': 'val2'}, 'key3': {'subkey1': 'val3', 'subkey3': 'val1'}}
I want to get a new dictionary by removing all the keys with the same value (keys are differents, values are the same)
For example: Input:
dct = {'key1' : [1,2,3], 'key2': [1,2,6], 'key3': [1,2,3]}
expected output:
{'key2': [1, 2, 6]}
key1 and key3 was deleted because they shared same values.
I have no idea about it?
You can do this by creating a dictionary based on the values. In this case the values are lists which are not hashable so convert to tuple. The values in the new dictionary are lists to which we append any matching key from the original dictionary. Finally, work through the new dictionary looking for any values where the list length is greater than 1 - i.e., is a duplicate. Then we can remove those keys from the original dictionary.
d = {'key1' : [1,2,3], 'key2': [1,2,6], 'key3': [1,2,3]}
control = {}
for k, v in d.items():
control.setdefault(tuple(v), []).append(k)
for v in control.values():
if len(v) > 1:
for k in v:
del d[k]
print(d)
Output:
{'key2': [1, 2, 6]}
I created a list of counts which holds information about how many times an item is in the dictionary. Then I copy only items which are there once.
a = {"key1": [1,2,3], "key2": [1,2,6], "key3": [1,2,3]}
# find how many of each item are there
counts = list(map(lambda x: list(a.values()).count(x), a.values()))
result = {}
#copy only items which are in the list once
for i,item in enumerate(a):
if counts[i] == 1:
result[item] = a[item]
print(result)
As given by the OP, the simplest solution to the problem:
dct = {'key1' : [1,2,3], 'key2': [1,2,6], 'key3': [1,2,3]}
print({k:v for k, v in dct.items() if list(dct.values()).count(v) == 1})
Output:
{'key2': [1, 2, 6]}
One loop solution:
dict_ = {'key1' : [1,2,3], 'key2': [1,2,6], 'key3': [1,2,3]}
key_lookup = {}
result = {}
for key, value in dict_.items():
v = tuple(value)
if v not in key_lookup:
key_lookup[v] = key
result[key] = value
else:
if key_lookup[v] is not None:
del result[key_lookup[v]]
key_lookup[v] = None
print(result)
Output:
{'key2': [1, 2, 6]}
dct = {'key1' : [1,2,3], 'key2': [1,2,6], 'key3': [1,2,3]}
temp = list(dct.values())
result = {}
for key, value in dct.items():
for t in temp:
if temp.count(t) > 1:
while temp.count(t) > 0:
temp.remove(t)
else:
if t == value:
result[key] = value
print(result)
Output:
{'key2': [1, 2, 6]}
I want to create a dictionary from a string that have key=value
s = "key1=value1 key2=value2 key3=value3"
print({r.split("=") for r in s})
Is it possible using dictionary comprehension? If yes, how?
You can first split on whitespace, then split on '='
>>> dict(tuple(i.split('=')) for i in s.split())
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
You could use map:
>>> s = "key1=value1 key2=value2 key3=value3"
>>> d = {k: v for k, v in map(lambda i: i.split('='), s.split())}
>>> {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
Consider a fixed list of keys and a dictionary of random key/values. I have to iterate through the dictionary and if a key is not in the list (keys that may or may not exist in the dictionary), then I add it to a new dictionary.
d = {'key1': '1', 'keyA': 'A', 'key2': '2', 'keyB': 'B', ...}
new_d = {}
for k, v in d.items():
if k not in ['key1', 'keyB', 'key_doesnotexist']:
new_d[k] = v
To optimize this, I thought about iterating through the list of keys first and popping anything that matches the list to get rid of the inner loop so something like this:
d = {'key1': '1', 'keyA': 'A', 'key2': '2', 'keyB': 'B', ...}
new_d = {}
for x in ['key1', 'keyB', 'key_doesnotexist']:
d.pop(x, default=None)
for k, v in d.items():
new_d[k] = v
Just wondering if there are any faster methods I should be aware of.
This might work faster:
d = {'key1': '1', 'keyA': 'A', 'key2': '2', 'keyB': 'B'}
new_d = {k: d[k] for k in d.keys() - ['key1', 'keyB', 'key_doesnotexist']}
Prints:
>>> new_d
{'keyA': 'A', 'key2': '2'}
Use a dict comprehension to select the items you want:
new_d = {k: v for k, v in d.items()
if k not in ['key1', 'keyB', 'key_doesnotexist']}
Or simply copy the dict and delete the unwanted entries:
import copy
new_d = copy.deepcopy(d)
for k in ['key1', 'keyB', 'key_doesnotexist']:
new_d.del(k)
I'm trying to invert items of a dictionary containing strings as key and string list as value:
dico = {'key1': [],
'key2': [],
'key3': ['value1', 'value1'],
'key4': ['value2', 'value2'],
'key5': ['value3'],
'key6': ['value1', 'value2', 'value3']}
new_dict = {}
for key, values in dico.items():
if values:
for value in values:
try:
if key not in new_dict[value]:
new_dict[value].append(key)
except KeyError:
new_dict[values[0]] = list(key)
else:
print('ERROR')
Here's the result expected:
#Expected
new_dict = {'value1': ['key3', 'key6'],
'value2': ['key4', 'key6'],
'value3': ['key5', 'key6']}
#Reality
new_dict = {'value1': ["k", "e", "y", "3", "k", "e", "y", "6"],
'value2': ["k", "e", "y", "4", "k", "e", "y", "6"],
'value3': ["k", "e", "y", "5", "k", "e", "y", "6"]}
I noticed if I change that:
new_dict[values[0]] = list(key)
by that:
new_dict[values[0]] = []
new_dict[values[0]].append(key)
It actually works but is there another way to do it in one line ?
You are turning your keys to lists:
new_dict[values[0]] = list(key)
That'll produce a list with individual characters. Use a list literal instead:
new_dict[values[0]] = [key]
You can use the dict.setdefault() method to handle missing keys in new_dict to simplify your code. It looks like you want to produce sets instead; sets track unique values and saves you having to do explicit tests for duplicates.
for key, values in dico.items():
for value in values:
new_dict.setdefault(value, set()).add(key)
You can always turn those sets back to lists afterwards:
new_dict = {key: list(values) for key, values in new_dict.items()}
Demo:
>>> dico = {'key1': [],
... 'key2': [],
... 'key3': ['value1', 'value1'],
... 'key4': ['value2', 'value2'],
... 'key5': ['value3'],
... 'key6': ['value1', 'value2', 'value3']}
>>> new_dict = {}
>>> for key, values in dico.items():
... for value in values:
... new_dict.setdefault(value, set()).add(key)
...
>>> new_dict
{'value3': set(['key6', 'key5']), 'value2': set(['key6', 'key4']), 'value1': set(['key3', 'key6'])}
>>> {key: list(values) for key, values in new_dict.items()}
{'value3': ['key6', 'key5'], 'value2': ['key6', 'key4'], 'value1': ['key3', 'key6']}
Iterate every item from main dico dictionary.
Check if value is present or not.
Iterate every item from value.
Use set method to remove duplicate values.
Add to new_dict dictionary where value is key and key is list value.
code:
dico = {'key1': [],
'key2': [],
'key3': ['value1', 'value1'],
'key4': ['value2', 'value2'],
'key5': ['value3'],
'key6': ['value1', 'value2', 'value3']}
new_dict = {}
for key, values in dico.items():
if values:
for value in set(values):
try:
new_dict[value].append(key)
except:
new_dict[value] = [key]
import pprint
pprint.pprint(new_dict)
Output:
$ python test.py
{'value1': ['key3', 'key6'],
'value2': ['key6', 'key4'],
'value3': ['key6', 'key5']}