Hi,
I want to do something like this:
I have two lists:

list1 = range(6)
list2 = range(10,15)

i want a dictionary like this:

adict ={0:10,
1:11,
2:12,
3:13,
4:14,
5:15
}

I did the following:

list1 = range(6)
list2 = range(10,15)
adict = dict(zip(list1,list2))

i get :
TypeError: list objects are unhashable
it didn't work.. any help pls..

Recommended Answers

All 14 Replies

I can not reproduce the problem, it must be somewhere else:

list1 = range(6)
list2 = range(10,15)
adict = dict(zip(list1,list2))
print(adict)
""" Output in Python 2.7.1 and 3.2
{0: 10, 1: 11, 2: 12, 3: 13, 4: 14}
"""

Hi,
I want to do something like this:
I have two lists:

list1 = range(6)
list2 = range(10,15)

i want a dictionary like this:

adict ={0:10,
1:11,
2:12,
3:13,
4:14,
5:15
}

I did the following:

list1 = range(6)
list2 = range(10,15)
adict = dict(zip(list1,list2))

i get :
TypeError: list objects are unhashable
it didn't work.. any help pls..

Same for me, I tried with 2.5, 2.6 and 3.1 and it works.

@ tonyjv
Thank you! I have python version 2.4.1 ,can that be posing the problem?

ok....
any other ways to do what I want?

I do not actually know, but could you not upgrade to a newer version? Usually only time you can not update the Python is when you have to use script Python, which is old version. It is also possible to install Python version in your user space and put proper 'she bang' line in beginning of your script if you are using Linux system without administration rights. It is possible to have many versions of Python in your system if necessary.

Here is a slower way to do it. Actually, there was an error in list2

>>> adict = {}
>>> list1 = range(6)
>>> list2 = range(10,16)
>>> for i, item in enumerate(list1):
...  adict[item] = list2[i]
... 
>>> adict
{0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15}

EDIT: Sorry, but the first version I posted was incorrect!

Little more readable if OK for Python2.4:

>>> adict = {}
>>> for key, item in enumerate(range(10,16)):
	adict[key] = item
	
>>> adict
{0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15}

@ Gribouillis

Thank you.. yes... i typed in list2 wrongly here, but that was not the problem. Thanks for the solution :)

If you have got answer to your question it is good to mark the thread solved and upvote/give reputation if you feel like it. Notice also that my alternative solution is now edited and functional, I was careless in writing original code. Sorry for that.

In my clutter of old USB flash cards I found a copy of Movable Python24, it does not give any problem with the original code

Taken by itself the original error message would indicate that at least one of items in list1 is list ie

print(filter(lambda x: isinstance(x, list), list1)

would not be empty.

OP says also 'i typed' not I copied in one post.

Thanks a lot to all those who have been posting reply to my threads and helping me throughout my projcet :) I would love to add reputation points to many, but unfortunately am not getting how to do it on forum :(

commented: You have manners +13

Reputation giving means just that:

  1. You upvote post by pushing up arrow from counter in top right corner
  2. From pop up you write comment to entry box and click 'Add reputation' (or something similar) button

Okkk.... got it now.. thank u tonyjv :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.