Kenny
Kenny

Reputation: 47

Comparing complicated lists

So I've got a function in python that has a list containing lists and I'm trying to compare the contents of the list to see if I have a duplicate within it, and then return True if there are duplicates. Essentially something like this;

def dupCheck():
    aList = ([3.3,3.2], [1,1], [3.3,3.2], [7,7])
    if duplicates in aList:
        return True
    return False

Upvotes: 0

Views: 76

Answers (3)

Jake Dube
Jake Dube

Reputation: 2990

A solution to the list of list problem:

def remove_doubles(a):
    """Removes list doubles in list where list(set(a)) will raise a TypeError."""
    clean_list = []
    for b in a:
        if b not in clean_list:
            clean_list += [b]
    return clean_list

Upvotes: 1

Padraic Cunningham
Padraic Cunningham

Reputation: 180441

You can do it lazily, returning True on the first duplicate in place of creating a whole set first:

def dup_check(lst):
    seen = set()
    for tup in map(tuple, lst):
        if tup in seen:
            return True
        seen.add(tup)
    return False

To keep lazy evaluation you should use itertools.imap in place of map using python2.

If you want a close to one liner solution, you could take advantage of the fact the set.add returns None and combine with any:

def dup_check(lst):
    seen = set()
    return any(tup in seen or seen.add(tup) for tup in map(tuple, lst))

seen.add(tup) is always going to be None and only if we have already added an identical tuple will tup in seen return True so any will either short-circuit on the first dupe or return False if there are no dupes.

Upvotes: 4

tobias_k
tobias_k

Reputation: 82929

Normally when checking whether a list has duplicates, you would just create a set from the list, thus removing the duplicates, and then compare the size of that set to the size of the original list. In this case this is not possible, as the nested lists are not hashable and can thus not be added to a set. However, if you always have a list of lists (and not, e.g. a list of lists and other things, or a list of lists of lists) then you can convert the sublists to tuples before putting them into the set.

>>> def dupCheck(lst):
...     return len(set(tuple(l) for l in lst)) != len(lst)  # duplicates -> true
...    
>>> dupCheck([[3.3,3.2], [1,1], [3.3,3.2], [7,7]])
True

Upvotes: 2

Related Questions