Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple keys per value

Is it possible to assign multiple keys per value in a Python dictionary. One possible solution is to assign value to each key:

dict = {'k1':'v1', 'k2':'v1', 'k3':'v1', 'k4':'v2'} 

but this is not memory efficient since my data file is > 2 GB. Otherwise you could make a dictionary of dictionary keys:

key_dic = {'k1':'k1', 'k2':'k1', 'k3':'k1', 'k4':'k4'} dict = {'k1':'v1', 'k4':'v2'} main_key = key_dict['k2'] value = dict[main_key] 

This is also very time and effort consuming because I have to go through whole dictionary/file twice. Is there any other easy and inbuilt Python solution?

Note: my dictionary values are not simple string (as in the question 'v1', 'v2') rather complex objects (contains different other dictionary/list etc. and not possible to pickle them)

Note: the question seems similar as How can I use both a key and an index for the same dictionary value? But I am not looking for ordered/indexed dictionary and I am looking for other efficient solutions (if any) other then the two mentioned in this question.

like image 928
d.putto Avatar asked Jul 12 '12 09:07

d.putto


People also ask

Can a value have multiple keys?

No, each key in a dictionary should be unique. You can't have two keys with the same value. Attempting to use the same key again will just overwrite the previous value stored. If a key needs to store multiple values, then the value associated with the key should be a list or another dictionary.

Can a value have multiple keys Python?

Python dictionary multiple key values In this example, if you want to get multiple keys-values then you need to associate an object with each key as a value. In the given dict the keys are strings and with every single key, we assign a list of numbers as a value.

How do I get the value of multiple keys in Python?

In python, if we want a dictionary in which one key has multiple values, then we need to associate an object with each key as value. This value object should be capable of having various values inside it. We can either use a tuple or a list as a value in the dictionary to associate multiple values with a key.

How do you add multiple key value pairs in a dictionary?

In Python, we can add multiple key-value pairs to an existing dictionary. This is achieved by using the update() method. This method takes an argument of type dict or any iterable that has the length of two - like ((key1, value1),) , and updates the dictionary with new key-value pairs.


2 Answers

What type are the values?

dict = {'k1':MyClass(1), 'k2':MyClass(1)} 

will give duplicate value objects, but

v1 = MyClass(1) dict = {'k1':v1, 'k2':v1} 

results in both keys referring to the same actual object.

In the original question, your values are strings: even though you're declaring the same string twice, I think they'll be interned to the same object in that case


NB. if you're not sure whether you've ended up with duplicates, you can find out like so:

if dict['k1'] is dict['k2']:     print("good: k1 and k2 refer to the same instance") else:     print("bad: k1 and k2 refer to different instances") 

(is check thanks to J.F.Sebastian, replacing id())

like image 130
Useless Avatar answered Sep 18 '22 06:09

Useless


Check out this - it's an implementation of exactly what you're asking: multi_key_dict(ionary)

https://pypi.python.org/pypi/multi_key_dict (sources at https://github.com/formiaczek/python_data_structures/tree/master/multi_key_dict)

(on Unix platforms it possibly comes as a package and you can try to install it with something like:

sudo apt-get install python-multi-key-dict 

for Debian, or an equivalent for your distribution)

You can use different types for keys but also keys of the same type. Also you can iterate over items using key types of your choice, e.g.:

m = multi_key_dict() m['aa', 12] = 12 m['bb', 1] = 'cc and 1' m['cc', 13] = 'something else'  print m['aa']   # will print '12' print m[12]     # will also print '12'  # but also: for key, value in m.iteritems(int):     print key, ':', value # will print:1 # 1 : cc and 1 # 12 : 12 # 13 : something else  # and iterating by string keys: for key, value in m.iteritems(str):     print key, ':', value # will print: # aa : 12 # cc : something else # bb : cc and 1  m[12] = 20 # now update the value print m[12]   # will print '20' (updated value) print m['aa']   # will also print '20' (it maps to the same element) 

There is no limit to number of keys, so code like:

m['a', 3, 5, 'bb', 33] = 'something'  

is valid, and either of keys can be used to refer to so-created value (either to read / write or delete it).

Edit: From version 2.0 it should also work with python3.

like image 29
formiaczek Avatar answered Sep 22 '22 06:09

formiaczek