Solved

Python:Remove duplicates without sorting?

Posted on 2016-10-27
5
101 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 35

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 35

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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 …

756 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