Solved

Python:Remove duplicates without sorting?

Posted on 2016-10-27
5
75 Views
Last Modified: 2016-10-31
Python:Remove duplicates without sorting?i want lines keep the order and not be sorted,anywaone have a  good python script?
thank you
0
Comment
Question by:john lambert
  • 2
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 41862941
def remove_dups(seq):  
    result = []
    for val in seq:
        if val not in result:
            result.append(val)
    return result

Open in new window


Not fast for huge lists, but easy to understand.

HTH,
Dan
1
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 41862956
A faster solution, taken from here:

def f7(seq):
    seen = set()
    seen_add = seen.add
    return [x for x in seq if not (x in seen or seen_add(x))]

Open in new window

1
 

Author Comment

by:john lambert
ID: 41862981
I want the most eficient solution, ok thank you
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41863002
the more_itertools library has a unique_everseen() method
https://pythonhosted.org/more-itertools/api.html
0
 

Author Closing Comment

by:john lambert
ID: 41867618
thank you........
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now