0

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.

Mark B
  • 205
  • 2
  • 7
  • `D['a':3]` What are you trying to do with this? – thefourtheye Feb 22 '14 at 04:15
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](http://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – thefourtheye Feb 22 '14 at 04:29
  • @thefourtheye maybe the OP wants something like pointer in C/Cpp, when modifying the values of the dict, make `L` knows that – zhangxaochen Feb 22 '14 at 04:30
  • @zhangxaochen But that is not very clear from the question. So, as per the site's standards that looks like an unclear question only :( – thefourtheye Feb 22 '14 at 04:31
  • @thefourtheye yes I'm just guessing ;) – zhangxaochen Feb 22 '14 at 04:35
  • I think it is pretty clear that he wants to do a c++ style reference (pointer) to one of the values in the dict. I don't think it is possible in python, is it? – SiddharthaRT Feb 22 '14 at 04:42

2 Answers2

0

You can't do that if the values of a dict are immutable(e.g., numbers, strings, tuples):

In [547]: d = {'a':1, 'b':2}

In [549]: l=d.values()
In [550]: d['a']=123

In [551]: l
Out[551]: [1, 2]

In [552]: d
Out[552]: {'a': 123, 'b': 2}

If some value in the dictionary is mutable(lists, etc.), you can get a "pointer" (or reference) of that value like in C/CPP, and modify it inplace:

In [560]: d = {'a':1, 'b':[]}
     ...: l = d.values()
     ...: print l
[1, []]

In [561]: d['b'].append(1) #modify [] inplace to [1]

In [562]: l #l "knows" that
Out[562]: [1, [1]]

In [563]: d
Out[563]: {'a': 1, 'b': [1]}

In [564]: l[1].append('asdf') #modify [1] inplace to [1, 'asdf']

In [565]: l
Out[565]: [1, [1, 'asdf']]

In [566]: d #d "knows" that
Out[566]: {'a': 1, 'b': [1, 'asdf']}

but if you change the reference of key 'b' in d, L will never know:

In [558]: d['b']=[3,4,5] #d['b'] refers to another list
     ...: d
Out[558]: {'a': 1, 'b': [3, 4, 5]}

In [559]: l
Out[559]: [1, [1]]
Community
  • 1
  • 1
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
0

If you want something similar like pointers to immutables, I recommend something like this:

>>> D  = {'a':1,'b':2}
>>> L=[lambda: D['a']]
>>> L[0]()
1
>>> D['a']=9
>>> L[0]()
9
koffein
  • 1,792
  • 13
  • 21