4

I would like to take a python dictionary of lists, convert the lists to numpy arrays, and restore them in the dictionary using list comprehension.

For example, if I had a dictionary

myDict = {'A':[1,2,3,4], 'B':[5,6,7,8], 'C':'str', 'D':'str'}

I wish to convert the lists under keys A and B to numpy arrays, but leave the other parts of the dictionary untouched. Resulting in

myDict = {'A':array[1,2,3,4], 'B':array[5,6,7,8], 'C':'str', 'D':'str'}

I can do this with a for loop:

import numpy as np

for key in myDict:
    if key not in ('C', 'D'):
        myDict[key] = np.array(myDict[key])

But is it possible to do this with list comprehension? Something like

[myDict[key] = np.array(myDict[key]) for key in myDict if key not in ('C', 'D')]

Or indeed what is the fastest most efficient way to achieve this for a large dictionaries of long lists. Thanks, labjunky

1
  • The answers of Haidro and Ashwini Chaudhary, below, are very similar and appeared concurrently. Commented Aug 29, 2013 at 4:11

3 Answers 3

3

With Python 2.7 and above, you can use a dictionary comprehension:

myDict = {'A':[1,2,3,4], 'B':[5,6,7,8], 'C':'str', 'D':'str'}
myDict = {key:np.array(val) if key not in {'C', 'D'} else val for key, val in myDict.iteritems()}

If you're below version 2.7 (and hence don't have dictionary comprehensions), you can do:

myDict = {'A':[1,2,3,4], 'B':[5,6,7,8], 'C':'str', 'D':'str'}
dict((key, np.array(val) if key not in {'C', 'D'} else val for key, val in myDict.iteritems())
Sign up to request clarification or add additional context in comments.

3 Comments

Nice, but I get <ipython-input-135-01de37e639d5> in <dictcomp>((key, val)) ----> 1 myDict = {key:np.array(val) if val not in {'C', 'D'} else val for key, val in myDict.iteritems()} TypeError: unhashable type: 'list'
Try ęlse key:val. It seems Python thinks in else val you want to use val as a key (that's why it says unhashable type.
@Cucu No, it was a simple typo of me checking if the list was in the set, when I should have been checking if the value (which can't be unhashable) was in the set
1

To change all items except 'C' and 'D':

>>> myDict = {'A':[1,2,3,4], 'B':[5,6,7,8], 'C':'str', 'D':'str'}
>>> ignore = {'C', 'D'}
>>> new_dict = {k : v if k in ignore else np.array(v) for k,v in myDict.iteritems()}

the above dict-comprehension returns a new dictionary, to modify the original dict, try:

#myDict.viewkeys() - ignore --> set(['A', 'B'])
for key in myDict.viewkeys() - ignore:
    myDict[key] = np.array(myDict[key])

or if you only want to change 'A' and 'B':

for key in {'A', 'B'}:
    myDict[key] = np.array(myDict[key])

1 Comment

spelling of 'ingore' second line
0

To

take a python dictionary of lists, convert the lists to numpy arrays, and restore them in the dictionary using list comprehension.

I would do:

myDict = {'A':[1,2,3,4], 'B':[5,6,7,8], 'C':'str', 'D':'str'}

def modifier(item):
    if type(item) == list:
        return np.array(item)
    else:
        return item

mod_myDict = {key: modifier(myDict[key]) for key in myDict.keys()}

The function then sets the restrictions you require in this case changing all lists into arrays. This returns:

{'A': array([1, 2, 3, 4]), 'B': array([5, 6, 7, 8]), 'C': 'str', 'D': 'str'}

Note: I believe this should be made shorter by using conditions in the if statement but I can't seem to figure it, something like

mod_myDict = {key: np.array(myDict[key]) if type(myDict[key])==list else key: myDict[key] for key in myDict.keys()}

That however, raises an error. Perhaps some more intelligent person than I knows why!

1 Comment

You may want something like the conditional comprehensions here: stackoverflow.com/questions/4260280/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.