0

I use to have two lists, files and g_list which were both regular lists. I wanted to remove the duplicates from files and have g_list match. I found this solution;

from collections import OrderedDict as odict     
od = odict.fromkeys(zip(files, g_list))
files, g_list = zip(*od)

I have since modified g_list to be a nested list, but now when I run the above code I get this TypeError:

File "/usr/lib/python2.7/collections.py", line 199, in fromkeys
  self[key] = value
File "/usr/lib/python2.7/collections.py", line 58, in __setitem__
  if key not in self:
TypeError: unhashable type: 'list'

How do I remedy this? Or is there another way to do what I desire?

Edit:

Input:

files = ['red', 'green', 'blue', 'green', 'yellow']
g_list = [['x','y'], ['z'], ['q','r','x'], ['z'], ['x', 'r']]

Desired Output:

files = ['red', 'green', 'blue', 'yellow']
g_list = [['x','y'], ['z'], ['q','r','x'], ['x', 'r']] 
6
  • Use a tuple instead. Can be as simple as tuple(my_list) Commented Nov 26, 2015 at 0:46
  • Possible duplicate of TypeError: unhashable type: 'dict', when dict used as a key for another dict Commented Nov 26, 2015 at 0:55
  • Using a tuple doesn't remove duplicates. A len(files) before and after yields the same result. Commented Nov 26, 2015 at 0:59
  • Can you edit your question to include an example of files and g_list, and what you want to have afterwards? Commented Nov 26, 2015 at 1:02
  • @p014k so if you just want to remove duplicates, use set(my_tuple) to remove the duplicates Commented Nov 26, 2015 at 1:27

2 Answers 2

1

This follows what you were trying to do, but without importing other libraries.

d = dict(zip(files, g_list)) 
files = d.keys()
g_list = d.values()
Sign up to request clarification or add additional context in comments.

Comments

1

Lists cannot be used as keys in dictionaries as they are mutable, they can be changed, so they can't be hashed. Well, they could be hashed, but that hash might change. As a dictionary relies on hashing its keys to be efficient, hashes must stay constant. The solution, then, is to use a tuple, which is exactly like a list except that it's immutable. To convert a list L to a tuple, simply do tuple(L).

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.