• Register
1 vote
32 views

Problem:

Can anyone tell me the best way to remove duplicates from list keep order in python.
Thank you.

 

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote

Answer:

The best way to remove duplicates from list keep order by using this code in python;

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

Explanation why use this:

I assigned seen.add to seen_add instead of just calling seen.add because Python is a dynamic language, and resolving seen.add each iteration is more costly than resolving a local variable. seen.add could have been changed between iterations, and the runtime isn't smart enough to rule that thing out and it has to check the object each time.

Hope this helps you.

11 6 4
34,950 points

Related questions

0 votes
1 answer 7 views
7 views
Problem: How to I Solve this: remove duplicates from list keep orde HELP! I need help understanding this: remove duplicates from list keep order Can someone please help solve this, I am new in python().
asked Mar 2 Mashhoodch 13k points
1 vote
1 answer 4 views
4 views
Problem: Please help me. How can I solve it? python remove duplicates from list keep order
asked Mar 19 MUHAMMAD MUNEEB 80k points
0 votes
1 answer 13 views
13 views
Problem: have a list of lists in Python: k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]] And I want to remove duplicate elements from it. Was if it a normal list not of lists I could used set. But unfortunate that list is not hashable and can't ... [3], [4]] I don't care about preserve order. Note: this question is similar but not quite what I need. Searched SO but didn't find exact duplicate.
asked Feb 19 Mashhoodch 13k points
1 vote
1 answer 31 views
31 views
Problem: Hello guys, I found python list is fun. I was doing some operation with a python list. Let me say, I have a python list looks: is_a_list = [1, 5, 3, 6, 3, 5, 6, 1] with mixed of duplicate values in it. Now, I am willing to remove all the duplicate elements from the list. How do you do that in an easiest and efficient way? I am eagerly waiting for your answers. Thanks.
asked Jun 25, 2020 adamSw 11.3k points
0 votes
1 answer 21 views
21 views
Problem: I have following list that contains duplicate entries: America Britain England America Pakistan America Pakistan Britain Europe I want to remove duplicates from this list. How can I do this?
asked Oct 23, 2020 Code Learner 9.2k points
0 votes
1 answer 2 views
2 views
Problem: I am a beginner. My teacher has given me an assignment in which there is a list with the list items having a \n. I am asked to remove that newline character from the list and print the results. As I have mentioned earlier that I am a beginner, I am facing ... list, given by the teacher: lst = [‘1\n’, ‘2\n’, ‘3\n’, ‘4\n’, ‘5\n’]
asked 6 days ago Code Learner 9.2k points
0 votes
1 answer 4 views
4 views
Problem: how to I Solve this: remove a list from a list python HELP! I need help understanding this: remove a list from a list python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 10 Mashhoodch 13k points
0 votes
1 answer 4 views
4 views
Problem: how to I Solve this: python remove item from list by value HELP! I need help understanding this: python remove item from list by value Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 7 Mashhoodch 13k points
0 votes
1 answer 12 views
12 views
Problem: case (in which it does not exist) shows the following error: Traceback (most recent call last): File "D:\zjm_code\a.py", line 6, in <module> b = a.index(6) ValueError: list.index(x): x not in list
asked Feb 21 Mashhoodch 13k points
0 votes
1 answer 16 views
16 views
Problem: How do I remove an element from a list by index in Python? I found the list.remove method, but say I want to remove the last element, how do I do this? It seems like the default remove searches the list, but I don't want any search to be performed.
asked Feb 19 Mashhoodch 13k points