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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:33:18+00:00 2026-05-16T12:33:18+00:00

There must be a simpler, more pythonic way of doing this. Given this list

  • 0

There must be a simpler, more pythonic way of doing this.

Given this list of pairs:

pp = [('a',1),('b',1),('c',1),('d',2),('e',2)]

How do I most easily find the first item in adjacent pairs where the second item changes (here, from 1 to 2). Thus I’m looking for [‘c’,’d’]. Assume there will only be one change in pair[1] for the entire list, but that it may be a string.

This code works but seems excruciatingly long and cumbersome.

for i, pair in enumerate(pp):
    if i == 0: 
        pInitial = pair[0] 
        sgInitial = pair[1]
    pNext = pair[0]
    sgNext = pair[1]
    if sgInitial == sgNext:
        sgInitial = sgNext
        pInitial = pNext
    else:
        pOne = pInitial
        pTwo = pNext
        x = [pOne, pTwo]
        print x
        break

Thanks
Tim

  • 1 1 Answer
  • 2 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-16T12:33:19+00:00Added an answer on May 16, 2026 at 12:33 pm
    import itertools as it
    
    pp = [('a',1),('b',1),('c',1),('d',2),('e',2)]
    
    # with normal zip and slicing
    for a,b in zip(pp,pp[1:]):
        if a[1] != b[1]:
            x=(a[0],b[0])
            print x
            break
    # with generators and izip
    iterfirst = (b for a,b in pp)
    itersecond = (b for a,b in pp[1:])
    iterfirstsymbol = (a for a,b in pp)
    itersecondsymbol = (a for a,b in pp[1:])
    iteranswer = it.izip(iterfirstsymbol, itersecondsymbol, iterfirst, itersecond)
    
    print next((symbol1, symbol2)
               for symbol1,symbol2, first, second in iteranswer
               if first != second)
    

    Added my readable generator version.

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

Sidebar

Related Questions

There must be a simple matlab way of doing this. I have a row
There must be an easier way or function to do this code here: #!/usr/bin/env
EDIT: There must be some way I can approach this without writing a whole
I am sure there must be a relatively straightforward way to do this, but
There must be a simple solution to this problem but for the love of
It feels like there must be some semi-simple solution to this, but I just
I know there must be a really simple answer to this question, but I
There must be something wrong. I don't know what? see this on jsfiddle .
I'm pretty sure there must be a way to launch spotify iphone app from
Please help me understand 2 things I found in this C code: First, there

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.