Solved

Python:Remove duplicates without sorting?

Posted on 2016-10-27
5
80 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

786 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