how to remove duplicate elements in a list

i like this post (click again to cancel)
0
i dont like this post (click again to cancel)

From a list L = ([1,2,3,4],[3,5,6,7],[7,8,9,10],[1,2,3.4]) remove duplicate sets here [1,2,3,4] has occurred twice and put only one making L = ([1,2,3,4],[3,5,6,7],[7,8,9,10])

asked Sep 17 '10

sriram gravatar image sriram
11 2 2 6

updated Apr 28 '11

Kelvin Li gravatar image Kelvin Li
423 9 16
L is a tuple. For a list, replace the outer parens with square brackets. ccanonc (Sep 17 '10)
Please change your tags. "urgentX" is more annoying than I am. ccanonc (Sep 17 '10)

4 Answers:

i like this answer (click again to cancel)
0
i dont like this answer (click again to cancel)

Maybe this might help:

sage: L = [[1,2,3,4], [3,5,6,7], [7,8,9,10], [1,2,3,4]]
sage: seen = []
sage: for e in L:
....:     if e in seen:
....:         continue
....:     seen.append(e)
....:     
sage: seen
[[1, 2, 3, 4], [3, 5, 6, 7], [7, 8, 9, 10]]
link

posted Sep 17 '10

mvngu gravatar image mvngu
316 7
i like this answer (click again to cancel)
0
i dont like this answer (click again to cancel)

If you're lazy and don't care about ordering in L:

sage: list(set(L))

Again, all of this is pure Python, so you might benefit from reading a tutorial on writing Python code.

link

posted Sep 17 '10

jsrn gravatar image jsrn
114 1 8
i like this answer (click again to cancel)
0
i dont like this answer (click again to cancel)

The page fastest way to uniquify a list in python addresses this; there, it's claimed that the following function is the "best" (fast, and maintains order):

def f5(seq, idfun=None): 
    # order preserving
    if idfun is None:
        def idfun(x): return x
    seen = {}
    result = []
    for item in seq:
        marker = idfun(item)
        # in old Python versions:
        # if seen.has_key(marker)
        # but in new ones:
        if marker in seen: continue
        seen[marker] = 1
        result.append(item)
    return result
link

posted Sep 17 '10

niles gravatar image niles
3329 5 38 92
http://nilesjohnson.net/
i like this answer (click again to cancel)
2
i dont like this answer (click again to cancel)

Sage has a uniq() function already. However, it requires that the data is hashable, so you can uniq-ify tuples and frozensets but not lists and sets:

sage: L = [(1, 2, 3, 4), (3, 5, 6, 7), (7, 8, 9, 10), (1, 2, 3, 4)]
sage: uniq(L)
[(1, 2, 3, 4), (3, 5, 6, 7), (7, 8, 9, 10)]
link

posted Sep 21 '10

Volker Braun gravatar image Volker Braun
2238 5 22 52

Your answer

Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
[hide preview]

Question tools

Tags:

Stats:

Asked: Sep 17 '10

Seen: 678 times

Last updated: Sep 21 '10

powered by ASKBOT version 0.7.22
Copyright Sage, 2010. Some rights reserved under creative commons license.