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 8277983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:51:33+00:00 2026-06-08T08:51:33+00:00

I want to remove certain duplicates in my python list. I know there are

  • 0

I want to remove certain duplicates in my python list.
I know there are ways to remove all duplicates, but I wanted to remove only consecutive duplicates, while maintaining the list order.

For example, I have a list such as the following:

list1 = [a,a,b,b,c,c,f,f,d,d,e,e,f,f,g,g,c,c]

However, I want to remove the duplicates, and maintain order, but still keep the 2 c’s and 2 f’s, such as this:

wantedList = [a,b,c,f,d,e,f,g,c]

So far, I have this:

z = 0
j=0
list2=[]
for i in list1:
    if i == "c":
        z = z+1
        if (z==1):
            list2.append(i)
        if (z==2):
            list2.append(i)
        else:
            pass
    elif i == "f":
        j = j+1
        if (j==1):
            list2.append(i)
        if (j==2):
            list2.append(i)
        else:
            pass
    else:
        if i not in list2:
            list2.append(i)  

However, this method gives me something like:

wantedList = [a,b,c,c,d,e,f,f,g]

Thus, not maintaining the order.

Any ideas would be appreciated! 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-06-08T08:51:35+00:00Added an answer on June 8, 2026 at 8:51 am

    Not completely sure if c and f are special cases, or if you want to compress consecutive duplicates only. If it is the latter, you can use itertools.groupby():

    >>> import itertools
    >>> list1
    ['a', 'a', 'b', 'b', 'c', 'c', 'f', 'f', 'd', 'd', 'e', 'e', 'f', 'f', 'g', 'g', 'c', 'c']
    >>> [k for k, g in itertools.groupby(list1)]
    ['a', 'b', 'c', 'f', 'd', 'e', 'f', 'g', 'c']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to remove every occurance of a certain value from a list. I
I got a list of lists and I want to remove a certain kind
I want to remove certain words from a string. The words I want to
I want to remove the braces so {test} becomes test. I tried {test}.gsub(/\{(.*)\}/,$1) while
I want to remove the decimal part of a number in a cell but
i want to remove the link from my header using CSS so that only
I want to find all nodes in a xml file that have a certain
In my List, I want to ignore certain entries(create list filter), by ignore I
I want to remove all images from a HTML page (actually tinymce user input)
I want to remove a IBOutlet UIimage from certain table cells: I have a

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.