Python Forum
Sets cannot contain lists, how to circumvent?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sets cannot contain lists, how to circumvent?
#1
Heart 
The easiest way to find out if a list is unique (does not contain duplicates) is to convert it into a set and check the size. However this does not work for a list of lists! Apparently set elements must be hashable... Seems a pity. Are there any plans to remove this constraint? A set of lists must surely have its uses. In the meantime, how could one circumvent this?
Reply
#2
Please describe in more detail what your are in the end trying to achieve. Very preferable with an example demonstrating your vision of comparing lists of lists.

Regards, noisefloor
Reply
#3
Do you understand why sets must contain only hashable elements?

While you mention wanting it only for a temporary purpose, sets could be longer lasting and having mutable, non-hashable elements could cause problems.

What would you expect this to output if sets could contain normal lists?

l1 = ["A"]
l2 = ["B"]
s = {l1, l2}
l2[0] = "B"
print(s)
As a workaround, if your list-of-lists is consistent (every element is a list, and every inner list consists of immutable objects (like numbers or strings), then you could do a set comprehension that converts the lists to tuples first.

>>> l = [[1, 2, 3], ["A", "B", "C"], [1, 2, 3]]
>>> {l}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
# Can't add the inner lists as set elements.   So instead we build a set where they are tuples...

>>> s = {tuple(x) for x in l}
>>> s
{(1, 2, 3), ('A', 'B', 'C')}
Gribouillis and buran like this post
Reply
#4
It is easy to make a list of lists free of duplicate lists:

from random import choice

# simple list
#listig = [i for i in range(9)] * 2
# convoluted list
listig = [[choice([1,2,3]) for j in range(3)] for i in range(20)]

# remove dups
def unique_me(alist):
    uni = []
    for e in alist :
        if not e in uni:
            uni.append(e)
    uni.sort()
    return uni

unique = unique_me(listig)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Circumvent the "access denied" page? Pedroski55 7 3,741 Jun-15-2024, 06:25 AM
Last Post: Pedroski55
  Peculiar pattern from printing of sets SahandJ 7 3,799 Dec-29-2021, 06:31 PM
Last Post: bowlofred
  How does one combine 2 data sets ? detlefschmitt 2 2,774 Sep-03-2021, 03:38 AM
Last Post: detlefschmitt
  Looping Through Large Data Sets JoeDainton123 10 7,408 Oct-18-2020, 02:58 PM
Last Post: buran
  Split dict of lists into smaller dicts of lists. pcs3rd 3 4,079 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  comprehension for sets Skaperen 2 2,980 Aug-07-2020, 10:12 PM
Last Post: Skaperen
  Sort sets by item values Sergey 4 100,044 Apr-19-2019, 10:50 AM
Last Post: Sergey
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 7,245 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Problem with character sets Pedroski55 4 7,280 Mar-04-2019, 02:35 AM
Last Post: snippsat
  merge 3 sql data sets to 1 librairy brecht83 0 2,893 Sep-26-2018, 10:13 PM
Last Post: brecht83

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020