I have a dictionary of several hundred key:value pairs, of the form...
In [1]: D = {'a':1, 'b':7, 'c':23, 'd':3}
I need to get the values from subsets of this dictionary. I'm currently storing them as keys, such as
In [2]: k = ['a', 'c']
Then I can get a list of corresponding key:value pairs using list comprehension...
In [5]: v = [(_k[0],_k[1]) for _k in D.items() if _k[0] in k]
In [6]: v
Out[6]: [('a', 1), ('c', 23)]
In my actual code, I will change a couple values in the dictionary, then update the key:value pairs and feed them into a function. I have to do this millions of times, so I was hoping I wouldn't have to remake the list at every step. However, it looks like ultimately I will have to update the list. Thanks for all of the help.
original post below...
I have a python dict, and I want to make a second list (or subdictionary) which points to the values in the first dictionary - ideally without recreating the list every time.
For example:
D = {'a':1, 'b':2}
L = [d['a']]
print L
... [1]
D['a':3]
print L
... [3]
I would think there should be an object which refers to the dictionary value, and then I could point to that object, but I can't figure out how.