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

  • SEARCH
  • Home
  • 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 8718537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:34:34+00:00 2026-06-13T06:34:34+00:00

Let’s say I have a list of strings: a = [‘a’, ‘a’, ‘b’, ‘c’,

  • 0

Let’s say I have a list of strings:

a = ['a', 'a', 'b', 'c', 'c', 'c', 'd']

I want to make a list of items that appear at least twice in a row:

result = ['a', 'c']

I know I have to use a for loop, but I can’t figure out how to target the items repeated in a row.
How can I do so?

EDIT: What if the same item repeats twice in a? Then the set function would be ineffective

a = ['a', 'b', 'a', 'a', 'c', 'a', 'a', 'a', 'd', 'd']
result = ['a', 'a', 'd']
  • 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-13T06:34:35+00:00Added an answer on June 13, 2026 at 6:34 am

    try itertools.groupby() here:

    >>> from itertools import groupby,islice
    >>> a = ['a', 'a', 'b', 'c', 'c', 'c', 'b']
    
    >>> [list(g) for k,g in groupby(a)]
    [['a', 'a'], ['b'], ['c', 'c', 'c'], ['b']] 
    
    >>> [k for k,g in groupby(a) if len(list(g))>=2]
    ['a', 'c']
    

    using islice() :

    >>> [k for k,g in groupby(a) if len(list(islice(g,0,2)))==2]
    >>> ['a', 'c']
    

    using zip() and izip():

    In [198]: set(x[0] for x in izip(a,a[1:]) if x[0]==x[1])
    Out[198]: set(['a', 'c'])
    
    In [199]: set(x[0] for x in zip(a,a[1:]) if x[0]==x[1])
    Out[199]: set(['a', 'c'])
    

    timeit results:

    from itertools import *
    
    a='aaaabbbccccddddefgggghhhhhiiiiiijjjkkklllmnooooooppppppppqqqqqqsssstuuvv'
    
    def grp_isl():
        [k for k,g in groupby(a) if len(list(islice(g,0,2)))==2]
    
    def grpby():
        [k for k,g in groupby(a) if len(list(g))>=2]
    
    def chn():
        set(x[1] for x in chain(izip(*([iter(a)] * 2)), izip(*([iter(a[1:])] * 2))) if x[0] == x[1])
    
    def dread():
        set(a[i] for i in range(1, len(a)) if a[i] == a[i-1])
    
    def xdread():
        set(a[i] for i in xrange(1, len(a)) if a[i] == a[i-1])
    
    def inrow():
        inRow = []
        last = None
        for x in a:
            if last == x and (len(inRow) == 0 or inRow[-1] != x):
                inRow.append(last)
            last = x
    
    def zipp():
        set(x[0] for x in zip(a,a[1:]) if x[0]==x[1])
    
    def izipp():
        set(x[0] for x in izip(a,a[1:]) if x[0]==x[1])
    
    if __name__=="__main__":
        import timeit
        print "islice",timeit.timeit("grp_isl()", setup="from __main__ import grp_isl")
        print "grpby",timeit.timeit("grpby()", setup="from __main__ import grpby")
        print "dread",timeit.timeit("dread()", setup="from __main__ import dread")
        print "xdread",timeit.timeit("xdread()", setup="from __main__ import xdread")
        print "chain",timeit.timeit("chn()", setup="from __main__ import chn")
        print "inrow",timeit.timeit("inrow()", setup="from __main__ import inrow")
        print "zip",timeit.timeit("zipp()", setup="from __main__ import zipp")
        print "izip",timeit.timeit("izipp()", setup="from __main__ import izipp")
    

    output:

    islice 39.9123107277
    grpby 30.1204478987
    dread 17.8041124706
    xdread 15.3691785568
    chain 17.4777339702
    inrow 11.8577565327           
    zip 16.6348844045
    izip 15.1468557105
    

    Conclusion:

    Poke’s solution is the fastest solution in comparison to other alternatives.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have thousands of users and I want to make the passwords
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have two text files that I need to extract data out
Let say I want to create an iOS app that download music files from
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option

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.