Solved

Python:Remove duplicates without sorting?

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python script details 2 81
Python Encoding Problem \u2013 4 120
python - JSON needs to be converted to a python dictionary (has some nested json) 3 78
Python Regex Problem 24 139
"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
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…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

809 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