I would like to take a python dictionary from lists, convert the lists to numpy arrays, and restore them to 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 want to convert the lists under keys A and B to numpy arrays, but not leave the rest of the dictionary intact. Result 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 can this be done with a list? Sort of
[myDict[key] = np.array(myDict[key]) for key in myDict if key not in ('C', 'D')]
Or indeed the fastest most effective way to achieve this for large dictionaries of long lists. Thanks labjunky