For example I have something like this:
{'key1': 'value', 'key2': 'value', 'key3': 'value','key4': 'value','key4': 'value'}
And have an array:
['key1', 'key3']
How to check if all keys from second array exists in first?
You can use all and a generator expression. This will iterate over your array and check that each key is in the keys of d. If at least one of the keys is missing then it'll return False.
d = {'key1': 'value', 'key2': 'value', 'key3': 'value','key4': 'value','key4': 'value'}
a = ['key1', 'key3']
all(key in d for key in a) # True
a2 = ['key1', 'key5']
all(key in d for key in a2) # False
You can use set.issubset() method to check
a = {'key1': 'value', 'key2': 'value', 'key3': 'value','key4': 'value','key4': 'value'}
b = ['key1', 'key3']
set(b).issubset(a)
#True
c = ['key1', 'key5']
set(c).issubset(a)
#False
Edit: apply issubset directly on a according to #Maroun Maroun comment
Another way (using set and issubset):
set(['key1', 'key3']).issubset(your_dict)
Use a loop and in operator with dict object:
adict = {'key1': 'value', 'key2': 'value', 'key3': 'value','key4': 'value','key4': 'value'}
keys_list = ['key1', 'key3']
for key in keys_list:
if key in adict:
print key
"in" can check if the key in a dict
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'}}
How would one go about cleaning a dictionary containing a variety of datatypes of nulls and empty lists, dicts etc. E.g.
raw = {'key': 'value', 'key1': [], 'key2': {}, 'key3': True, 'key4': None}
To:
refined = {'key': 'value', 'key3': true}
Because of the mixed nature of data types in the dictionary, using:
refined = {k:v for k,v in processed.items() if len(v)>0}
throws a
TypeError: object of type 'bool' has no len()
Is there a solution to make a second conditional based on type(v) is bool?
Edit: I've found the issue I was encountering employing solutions was a result of the structure of the data, asking a separate question to deal with that.
You can try this.
refined={k:v for k,v in raw.items() if v or isinstance(v,bool)}
raw={'key': 'value',
'key1': [],
'key2': {},
'key3': True,
'key4': None,
'key5': False}
refined={k:v for k,v in raw.items() if v or isinstance(v,bool)}
#{'key': 'value', 'key3': True, 'key5': False}
How about
refined = {k:v for k, v in processed.items() v is not None and (type(v) not in (list, dict) or len(v) > 0)}
I have a list of dictionaries and a function that can extract a value from each of those dictionaries in the list. The goal is that i get a dictionary where the keys are the values that are returned by the given function when i pass it the dictionaries from the given list of dictionaries. The according values in the returned dictionary should be the subset of dictionaries from the original list of dictionaries for which the given function returned the according key.
I know this explanation is very confusing, so I'm showing it in an implementation:
keygen = lambda x: x['key']
data = [{'key': 'key1',
'data': 'value2'},
{'key': 'key3',
'data': 'value2'},
{'key': 'key2',
'data': 'value2'},
{'key': 'key2',
'data': 'value2'},
{'key': 'key1',
'data': 'value2'}]
def merge_by_keygen(data, keygen):
return_value = {}
for dataset in data:
if keygen(dataset) not in return_value.keys():
return_value[keygen(dataset)] = []
return_value[keygen(dataset)].append(dataset)
return return_value
merge_by_keygen(data, keygen)
returns:
{'key3': [{'data': 'value2', 'key': 'key3'}],
'key2': [{'data': 'value2', 'key': 'key2'}, {'data': 'value2', 'key': 'key2'}],
'key1': [{'data': 'value2', 'key': 'key1'}, {'data': 'value2', 'key': 'key1'}]}
I'm looking for a nicer and more compact implementation of the same logic, like some dictionary/list comprehensions. Thanks!
This is an ideal problem to be handled by itertools.groupby
Implementation
from itertools import groupby
from operator import itemgetter
groups = groupby(sorted(data, key = itemgetter('key')), key = itemgetter('key'))
data_dict = {k : list(g) for k, g in groups}
or if you prefer one-liner
data_dict = {k : list(g)
for k, g in groupby(sorted(data,
key = itemgetter('key')),
key = itemgetter('key'))}
Output
{'key1': [{'data': 'value2', 'key': 'key1'},
{'data': 'value2', 'key': 'key1'}],
'key2': [{'data': 'value2', 'key': 'key2'},
{'data': 'value2', 'key': 'key2'}],
'key3': [{'data': 'value2', 'key': 'key3'}]}
If you don't mind using a third-party package, this is easily done with toolz.groupby:
>>> import toolz
>>> toolz.groupby(keygen, data)
{'key1': [{'data': 'value2', 'key': 'key1'},
{'data': 'value2', 'key': 'key1'}],
'key2': [{'data': 'value2', 'key': 'key2'},
{'data': 'value2', 'key': 'key2'}],
'key3': [{'data': 'value2', 'key': 'key3'}]}
The same result is also obtained with toolz.groupby('key', data)
I don't think this is amenable to a comprehension, but you can make it tidier using a collections.defaultdict(list) instance:
import collections
def merge_by_keygen(data, keygen):
return_value = collections.defaultdict(list)
for dataset in data:
key = keygen(dataset)
return_value[key].append(dataset)
return return_value
That looks pretty clean to me - you could mess around with ways to move where you call the keygen function if you like but I think you'd probably lose clarity.
I think this does it
return_value = {}
for d in data:
return_value.setdefault(keygen(d), []).append(d)
You can write it in a list comprehension, but it's ugly to use the side effects of a list comprehension to affect data and then build up a list of None results and throw it away...
r = {}
[r.setdefault(keygen(d), []).append(d) for d in data]
The core of your function all mashes down into the dictionary setdefault method. All three lines about calling the keygen, checking if the key is in the return dictionary, if it's not create an empty list, store the empty list in the dictionary, then get query the dictionary again to get the list ready to append to it - all done by setdefault().
I'm new to Python and am having to learn by trial and error, but I haven't been able to find a solution to the problem I'm having.
I have a dictionary that looks something like this:
myDict = {'key1': ['item1', 'item2', 'item3'], 'key2': ['item4', 'item5', 'item6'],
'key3': 'item7', 'key4': 'item8', 'key5': ['item1', 'item2', 'item3'], 'key6': 'item7'}
I need to remove duplicate values from the dictionary and replace them with an empty value (""). Found a couple solution on here but they are working as intended
for key, value in myDict.items():
if values not in key newDict.values():
myDict[key] = value
else:
myDict[key] = ""
print newDict
This is removing all the values and is outputting
# newDict:{key1: '', key2: '', key3: '', key4: '', key5: '', key6: '')
I'm looking for the output to be
# newDict = {'key1': '', 'key2':['item4', 'item5', 'item6'], 'key3': '', 'key4':
'item8', key5: ['item1', 'item2', 'item3'], 'key6': 'item7'}
You have the right overall idea, but there are three problems with your code:
You're storing values back into myDict instead of into newDict.
On line 2, you're checking values instead of value.
Also on line 2, key shouldn't be there, and throws a SyntaxError.
Here is the correct code:
newDict = {}
for key, value in myDict.iteritems():
if value not in newDict.values():
newDict[key] = value
else:
newDict[key] = ""
print newDict
If you're not in the anti-ternary operator camp, you could also shorten it to this:
newDict = {}
for key, value in myDict.iteritems():
newDict[key] = value if value not in newDict.values() else ""
print newDict
Or, if you would rather just remove the values from the original dict (myDict) instead of building a new one (newDict), you could do this:
foundValues = []
for key, value in myDict.iteritems():
if value not in foundValues:
foundValues.append(myDict[key])
else:
myDict[key] = ""
print myDict
If you need duplicate values removed in a specific order, check out OrderedDicts.
Update:
In light of the updated requirements -- that values be removed from the original dict, starting from the beginning -- if you're able to simply initialize myDict with an OrderedDict instead of a dict, all you need to do is replace this:
myDict = {'key1': ['item1', 'item2', 'item3'], 'key2': ['item4', 'item5', 'item6'], 'key3': 'item7', 'key4': 'item8', 'key5': ['item1', 'item2', 'item3'], 'key6': 'item7'}
with this:
from collections import OrderedDict
…
myDict = OrderedDict([('key1', ['item1', 'item2', 'item3']), ('key2', ['item4', 'item5', 'item6']), ('key3', 'item7'), ('key4', 'item8'), ('key5', ['item1', 'item2', 'item3']), ('key6', 'item7')])
and then use the same code provided above.
This does it:
myDict_values = myDict.values() # better than calling this numerous times
for key in myDict.keys():
if myDict_values.count(myDict[key]) > 1: myDict[key] = ""
This won't guarantee that key5 will be blank instead of key1, because dictionaries are not ordered.
I have this list:
single = ['key1', 'value1', 'key2', 'value2', 'key3', 'value3']
What's the best way to create a dictionary from this?
Thanks.
>>> single = ['key1', 'value1', 'key2', 'value2', 'key3', 'value3']
>>> dict(zip(single[::2], single[1::2]))
{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}
Similar to SilentGhost's solution, without building temporary lists:
>>> from itertools import izip
>>> single = ['key1', 'value1', 'key2', 'value2', 'key3', 'value3']
>>> si = iter(single)
>>> dict(izip(si, si))
{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}
This is the simplest I guess. You will see more wizardry in solution here using list comprehension etc
dictObj = {}
for x in range(0, len(single), 2):
dictObj[single[x]] = single[x+1]
Output:
>>> single = ['key1', 'value1', 'key2', 'value2', 'key3', 'value3']
>>> dictObj = {}
>>> for x in range(0, len(single), 2):
... dictObj[single[x]] = single[x+1]
...
>>>
>>> dictObj
{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}
>>>
L = ['key1', 'value1', 'key2', 'value2', 'key3', 'value3']
d = dict(L[n:n+2] for n in xrange(0, len(L), 2))
>>> single = ['key', 'value', 'key2', 'value2', 'key3', 'value3']
>>> dict(zip(*[iter(single)]*2))
{'key3': 'value3', 'key2': 'value2', 'key': 'value'}
Probably not the most readable version though ;)
You haven't specified any criteria for "best". If you want understandability, simplicity, easily modified to check for duplicates and odd number of inputs, works with any iterable (in case you can't find out the length in advance), NO EXTRA MEMORY USED, ... try this:
def load_dict(iterable):
d = {}
pair = False
for item in iterable:
if pair:
# insert duplicate check here
d[key] = item
else:
key = item
pair = not pair
if pair:
grumble_about_odd_length(key)
return d