0

I have a list like this-

send_recv_pairs = [(['produce_send'], ['consume_recv']), (['Send'], ['Recv']), (['sender2'], ['receiver2'])]

I want something like

[ {['produce_send']:['consume_recv']},{['Send']:['Recv']},{['sender2']:['receiver2']}

How to do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
megna
  • 215
  • 3
  • 16

7 Answers7

4

You can not use list as the key of dictionary. This Article explain the concept,

https://wiki.python.org/moin/DictionaryKeys

To be used as a dictionary key, an object must support the hash function (e.g. through hash), equality comparison (e.g. through eq or cmp), and must satisfy the correctness condition above.

And

lists do not provide a valid hash method.

>>> d = {['a']: 1}

    TypeError: unhashable type: 'list'

If you want to specifically differentiate the key values you can use tuple as they hash able

{ (i[0][0], ): (i[1][0], )  for i in send_recv_pairs}

{('Send',): ('Recv',),
 ('produce_send',): ('consume_recv',),
 ('sender2',): ('receiver2',)}
Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24
1

You can't have lists as keys, only hashable types - strings, numbers, None and such. If you still want to use a dictionary knowing that, then:

d={}
for tup in send_recv_pairs:
    d[tup[0][0]]=tup[1]

If you want the value to be string as well, use tup[1][0] instead of tup[1]

As a one liner:

d={tup[0][0]]:tup[1] for tup in list}  #tup[1][0] if you want values as strings  
Jay
  • 2,535
  • 3
  • 32
  • 44
0

You can check it over here, in the second way of creating distionary. https://developmentality.wordpress.com/2012/03/30/three-ways-of-creating-dictionaries-in-python/

A Simple way of doing it,

First of all, your tuple is tuple of lists, so better change it to tuple of strings (It makes more sense I guess)
Anyway simple way of working with your current tuple list can be like :

mydict = {}
for i in send_recv_pairs:
    print i
    mydict[i[0][0]]= i[1][0]   

As others pointed out, you cannot use list as key to dictionary. So the term i[0][0] first takes the first element from the tuple - which is a list- and then the first element of list, which is the only element anyway for you.

jkhadka
  • 2,443
  • 8
  • 34
  • 56
0

Do you mean like this?

send_recv_pairs = [(['produce_send'], ['consume_recv']),
                   (['Send'], ['Recv']),
                   (['sender2'], ['receiver2'])]

send_recv_dict = {e[0][0]: e[1][0] for e in send_recv_pairs}

Resulting in...

>>> {'produce_send': 'consume_recv', 'Send': 'Recv', 'sender2': 'receiver2'}
flevinkelming
  • 690
  • 7
  • 8
0

As mentioned in other answers, you cannot use a list as a dictionary key as it is not hashable (see links in other answers).

You can therefore just use the values in your lists (assuming they stay as simple as in your example) to create the following two possibilities:

send_recv_pairs = [(['produce_send'], ['consume_recv']), (['Send'], ['Recv']), (['sender2'], ['receiver2'])]


result1 = {}

for t in send_recv_pairs:

    result1[t[0][0]] = t[1]

# without any lists

result2 = {}
for t in send_recv_pairs:

    result2[t[0][0]] = t[1][0]

Which respectively gives:

>>> result1
{'produce_send': ['consume_recv'], 'Send': ['Recv'], 'sender2': ['receiver2']}
>>> result2
{'produce_send': 'consume_recv', 'Send': 'Recv', 'sender2': 'receiver2'}
n1k31t4
  • 2,745
  • 2
  • 24
  • 38
0

Try like this:

res = { x[0]: x[1] for x in pairs } # or x[0][0]: x[1][0] if you wanna store inner values without list-wrapper

It's for Python 3 and when keys are unique. If you need collect list of values per key, instead of single value, than you may use something like itertools.groupby or map+reduce. Wrote about this in comments and I'll provide example.

And yes, list cannot store key-values, only dict's, but maybe it's just typo in question.

Green_Wizard
  • 795
  • 5
  • 11
0

You can not use list as the dictionary key, but instead you may type-cast it as tuple to create the dict object.

Below is the sample example using a dictionary comprehension:

>>> send_recv_pairs = [(['produce_send'], ['consume_recv']), (['Send'], ['Recv']), (['sender2'], ['receiver2'])]

>>> {tuple(k): v for k, v in send_recv_pairs}
{('sender2',): ['receiver2'], ('produce_send',): ['consume_recv'], ('Send',): ['Recv']}

For details, take a look at: Why can't I use a list as a dict key in python?


However if your nested tuple pairs were not list, but any other hashable object pairs, you may have type-casted it to dict for getting the desired result. For example:

>>> my_list = [('key1', 'value1'), ('key2', 'value2')]

>>> dict(my_list)
{'key1': 'value1', 'key2': 'value2'}
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126