Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6144959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:43:33+00:00 2026-05-23T18:43:33+00:00

Possible Duplicates: How do you remove duplicates from a list in Python whilst preserving

  • 0

Possible Duplicates:
How do you remove duplicates from a list in Python whilst preserving order?
In Python, what is the fastest algorithm for removing duplicates from a list so that all elements are unique while preserving order?

I was wondering if there was a function which does the following:

Take a list as an argument:

list = [ 3 , 5 , 6 , 4 , 6 , 2 , 7 , 6 , 5 , 3 ]

and deletes all the repeats in the list to obtain:

list = [ 3 , 5 , 6 , 4 , 2 , 7 ]

I know you can convert it into a dictionary and use the fact that dictionaries cannot have repeats but I was wondering if there was a better way of doing it.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:43:34+00:00Added an answer on May 23, 2026 at 6:43 pm

    Please see the Python documentation for three ways to accomplish this. The following is copied from that site. Replace the example ‘mylist’ with your variable name (‘list’).

    First Example: If you don’t mind reordering the list, sort it and then scan from the end of the list, deleting duplicates as you go:

    if mylist:
        mylist.sort()
        last = mylist[-1]
        for i in range(len(mylist)-2, -1, -1):
            if last == mylist[i]:
                del mylist[i]
            else:
                last = mylist[i]
    

    Second Example: If all elements of the list may be used as dictionary keys (i.e. they are all hashable) this is often faster:

    d = {}
    for x in mylist:
        d[x] = 1
    mylist = list(d.keys())
    

    Third Example: In Python 2.5 and later:

    mylist = list(set(mylist))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicates: Exception during iteration on collection and remove items from that collection How
Possible Duplicate: Can you remove elements from a std::list while iterating through it? I
Possible Duplicates: How do I remove objects from an Array in java? Removing an
Possible Duplicate: Remove duplicates from array I have a list of items. I want
Possible Duplicate: Can you remove elements from a std::list while iterating through it? I
Possible Duplicate: Remove from the array elements that are repeated. in Ruby I would
Possible Duplicate: How to remove elements from an array I have an List with
Possible Duplicate: Remove items from a list while iterating in Python I want to
Possible Duplicate: Remove duplicates from a List<T> in C# i have a List like
Possible Duplicate: Remove elements from array? how would I remove all array elements but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.