2

Sorry if a repeated question, new to programming couldn't find any appropriate answer. I am trying to convert a list to dictionary. key being x[1] and values as x[2]

Just to be clear I have a n*3 array and need to convert it to a dictionary with 2nd column as keys and 3rd column as values.

I tried:

for entry in data:
     keys=entry[1]
     values=entry[2]

source = dict.fromkeys(keys, values)

got error 'type list not hashable'

Went through question 4576115. However, I need to iterate through each element of a 3000+ *3 array with most elements themselves being list and thereafter setting the 2nd element of each entry as a key and the 3rd as a value. just to amplify, also tried

b = {data[i][1]: data[i][2] for i in range(0, len(data))}

and for entry in data:

keys=entry[1]
values=entry[2]
b.update(keys,values)

and

keyList.append(keys)
valueList.append(values)
b = dict(zip(keyList,valueList))

and

b={entry[1]:entry[2] for entry in data} 

same issue- error 'list' not hashable

OK sorry. Got it. The issue is that the key themselves are lists. I was looking at the array and missed the element

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
anand
  • 269
  • 2
  • 3
  • 9
  • Do you mean you have a long list, where every two indices is a key:value pair? [k1, v1, k2, v2, k3, v3, ...] – gregb212 Sep 18 '13 at 16:23
  • Who is voting to reopen? This is a clear duplicate. – Marcin Sep 18 '13 at 16:30
  • 1
    This is not a duplicate of question 4576115. – RichieHindle Sep 18 '13 at 16:30
  • @RichieHindle Only in that that answers for this are slightly simpler. – Marcin Sep 18 '13 at 16:32
  • 1
    @Marcin reread each question, the initial state is different. This is not a duplicate of 4576115. – cmd Sep 18 '13 at 17:33
  • @cmd So what? The solutions are substantially the same; the only difference here is that the solutions omit a step. – Marcin Sep 18 '13 at 17:34
  • 1
    @Marcin except that those answers dont answer this question, sure they are exactly the same. I am sure this question or one more similar has been asked, find that one to dup it. duping this one with the wrong answers is less then useful – cmd Sep 18 '13 at 17:37
  • @cmd Well, I think they do answer this question. – Marcin Sep 18 '13 at 17:39
  • Could you please show us the structure of "DATA"? if the item you want is the key of the dictionary is a list lists does not not have a hash, you can do is convert the list to a tuple, tuples have hashes. – Zokis Sep 19 '13 at 14:42

1 Answers1

8

Did not quite understand, but would not this what you want?

>>> a = [[1,2,3],[2,3,4],[4,5,6]]
>>> b = {x[1]:x[2] for x in a}
>>> b
{2: 3, 3: 4, 5: 6}
>>> 
Zokis
  • 390
  • 6
  • 12
  • Sequence assignment is usually preferable. – Marcin Sep 18 '13 at 16:33
  • that form the code runs faster: http://pythonfasterway.uni.me/#test24 – Zokis Sep 18 '13 at 17:05
  • Apart from that site being hideously unusable, you're comparing apples to oranges. Try actually comparing similar things: `{x[1]:x[2] for x in a}` to `{k:v for x,k,v in a}` – Marcin Sep 18 '13 at 17:14
  • you're right unpacking is even faster! tidy in http://pythonfasterway.uni.me/#test24 – Zokis Sep 18 '13 at 17:22
  • My testing shows that unpacking is *very slightly* slower: http://ideone.com/WoYGf4 However, to choose based on this kind of time difference is the definition of premature optimization. – Marcin Sep 18 '13 at 17:27
  • strange my tests show the opposite: pythonfasterway.uni.me/#test24 – Zokis Sep 18 '13 at 17:29
  • Possibly you're using a different version of python. Again, that's another reason not to prematurely optimise this. – Marcin Sep 18 '13 at 17:31