Split dictionary into two sub-dictionaries using a string - python

I have a dictionary and I want to split it into two sub-dictioaries based on a string.
Is there a nicer (more pythonic) way to do it than this:
dict_1 = {k:v for (k,v) in initial_dict.iteritems() if string in k}
dict_2 = {k:v for (k,v) in initial_dict.iteritems() if string not in k}

dict_1 = {key:initial_dict.pop(key) for key in initial_dict if string in key}
dict_2 = initial_dict

I'll vote for your original. Clear, short, and only slightly inefficient
There is a way to do it without referencing or testing elements of initial_dict more than once, which is quite Pythonic if Pythonic means knowing that int(False)==0 and int(True)==1
dict1, dict2 = {}, {}
for k,v in initial_dict.items(): (dict2,dict1)[string in k][k] = v
Like I said, I prefer the questioner's way!
By the way, if you have to perform an n-way partition, dict_list[i][key]=v inside a loop that fetches or generates key,i and v starts to look a lot better than a multi-way if ... elif ... elif ...

Related

filter a dictionary by key less than some range of values

I have a dictionary like this,
d = {1:'a', 2:'b', 3:'c', 4:'d'}
Now I want to filter the dictionary where the key should be more than 1 and less than 4 so the dictionary will be,
d = {2:'b', 3:'c'}
I could do this using a for loop, iterating over all the keys. but the execution time will be more looking for some fastest way to do this more efficiently in pythonic way.
you can try below code:
d = {k:v for k,v in d.items() if 1<k<4}
Pythonic way would be to use a dictionary comprehension:
{key: value for key, value in d.items() if 1 < key < 4}
It's readable enough: for each key and value in the items of the dictionary, keep those key: value pairs that have their keys between 1 and 4.
More pythonic way would be a dictionary comprehension
d = {k: v for (k, v) in d.items() if k > 1 and k < 4}
If the efficiency is a bottleneck, you may want to try to use some tree based structure instead of a dictionary that is hash based.
Python dictionaries use hashes of their keys to efficiently lookup and store data. Unfortunately, that means that they don't allow you use the numeric properties of those keys to select data (the way you might be able to do using a slice to index a list).
So I think the best way to do what you want is probably just a dictionary comprehension that tests each key against your boundary values:
d = {key: value for key, value in d.items() if 1 < key < 4}

Processing dictionary of dictionaries

I have a dictionary - where the values are dictionaries themselves.
How do I extract the unique set of the values from the child dictionaries in the most efficient way?
{ 'A':{'A1':'A1V','B2':'A2V'..},
'B':{'B1':'B1V','B2':'B2V'...},
...}
Expected output:
['A1V','A2V','B1V','B2V'...]
In a single line:
>>> [val for dct in x.values() for val in dct.values()]
['A1V', 'A2V', 'B2V', 'B1V']
Assuming you named your dict of dict x.
You mentioned unique, in that case replace the list-comprehension by a set-comprehension:
>>> {val for dct in x.values() for val in dct.values()} # curly braces!
{'A1V', 'A2V', 'B1V', 'B2V'}
uniques = set()
for ukey, uvalue in outerdic.items():
for lkey, lvalue in uvalue.items():
uniques.add(lvalue)
print uniques
Using a set should work. New to stackoverflow, trying to figure out how syntax highlighting works.
This assumes that the dictionary is called outerdic.
dictionary = { 'A':{'A1':'A1V','B2':'A2V'},'B':{'B1':'B1V','B2':'B2V'}}
for key in dictionary.keys() :
dict1 = dictionary[key]
for key1 in dict1.keys():
print(dict1[key1])
Tried to keep it as simple as possible.

how to combine the common key and join the values in the dictionary python

I have one list which contain a few dictionaries.
[{u'TEXT242.txt': u'work'},{u'TEXT242.txt': u'go to work'},{u'TEXT1007.txt': u'report'},{u'TEXT797.txt': u'study'}]
how to combine the dictionary when it has the same key. for example:
u'work', u'go to work'are under one key:'TEXT242.txt', so that i can remove the duplicated key.
[{u'TEXT242.txt': [u'work', u'go to work']},{u'TEXT1007.txt': u'report'},{u'TEXT797.txt': u'study'}]
The setdefault method of dictionaries is handy here... it can create an empty list when a dictionary key doesn't exist, so that you can always append the value.
dictlist = [{u'TEXT242.txt': u'work'},{u'TEXT242.txt': u'go to work'},{u'TEXT1007.txt': u'report'},{u'TEXT797.txt': u'study'}]
newdict = {}
for d in dictlist:
for k in d:
newdict.setdefault(k, []).append(d[k])
from collections import defaultdict
before = [{u'TEXT242.txt': u'work'},{u'TEXT242.txt': u'go to work'},{u'TEXT1007.txt': u'report'},{u'TEXT797.txt': u'study'}]
after = defaultdict(list)
for i in before:
for k, v in i.items():
after[k].append(v)
out:
defaultdict(list,
{'TEXT1007.txt': ['report'],
'TEXT242.txt': ['work', 'go to work'],
'TEXT797.txt': ['study']})
This technique is simpler and faster
than an equivalent technique using dict.setdefault()

Efficient way of removing keys of certain prefix from python dictionary

I have a python dictionary which looks something like this;
{'Prefix_1':'12', 'Prefix_2':'11', 'Prefix_3':'14', '1':'241', '2':'312', '3':'421'
}
I want to remove the key-value pairs of those with keys that start with 'Prefix'. The result should be a dictionary that look like this;
{'1':'241', '2':'312', '3':'421'
}
My current way of doing so is to remove each pair one by one by using del dictionary['Prefix_X']. What are more efficient ways of doing so?
I am using python 2.7
Since the other answers all use dict comprehension to create a new dict and leave the original dict untouched, I'll give one that change the dict in place:
for k in d.keys():
if k.startswith('Prefix'):
d.pop(k)
Is there a better way?
Let's say there are N keys in the dictionary, to find all keys with the given prefix, you'll have to iterate over all the keys, and this is of O(N) time complexity.
Then you'll need to delete them one by one, in the worst case all of them are with the given prefix, so this is also of O(N) time complexity.
The total time complexity if O(N).
You could use a dict comprehension over the original dictionary:
D = {'Prefix_1':'12', 'Prefix_2':'11', 'Prefix_3':'14', '1':'241', '2':'312', '3':'421'
NewDict = {k: D[k] for k in D if not k.startswith('Prefix')}
NewDict
{'2': '312', '3': '421', '1': '241'}
use dictionary comprehensions
{k:v for k, v in d.items() if not k.startswith('Prefix')}
In [44]: {k:v for k, v in d.items() if not k.startswith('Prefix')}
Out[44]: {'1': '241', '2': '312', '3': '421'}
>>> z = {'Prefix_1':'12', 'Prefix_2':'11', 'Prefix_3':'14', '1':'241', '2':'312', '3':'421'}
>>> {k:v for k,v in z.items() if not k.startswith('Prefix')}
{'1': '241', '3': '421', '2': '312'}
d1 = {'Prefix_1':'12', 'Prefix_2':'11', 'Prefix_3':'14', '1':'241', '2':'312', '3':'421'}
d2 = {k: v for k, v in d1.iteritems() if not k.startswith('Prefix')}
print d1
print d2
Try to this.
for key in d.keys():
if 'Prefix' in key:
d.pop(key)

Selecting dictionary items by key efficiently in Python

suppose I have a dictionary whose keys are strings. How can I efficiently make a new dictionary from that which contains only the keys present in some list?
for example:
# a dictionary mapping strings to stuff
mydict = {'quux': ...,
'bar': ...,
'foo': ...}
# list of keys to be selected from mydict
keys_to_select = ['foo', 'bar', ...]
The way I came up with is:
filtered_mydict = [mydict[k] for k in mydict.keys() \
if k in keys_to_select]
but I think this is highly inefficient because: (1) it requires enumerating the keys with keys(), (2) it requires looking up k in keys_to_select each time. at least one of these can be avoided, I would think. any ideas? I can use scipy/numpy too if needed.
dict((k, mydict[k]) for k in keys_to_select)
if you know all the keys to select are also keys in mydict; if that's not the case,
dict((k, mydict[k]) for k in keys_to_select if k in mydict)

Categories

Resources