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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:09:06+00:00 2026-06-14T09:09:06+00:00

Possible Duplicate: Iterate a list as pair (current, next) in Python I have a

  • 0

Possible Duplicate:
Iterate a list as pair (current, next) in Python

I have a list like this:

list = [A, B, C, D, E, F, G]

How can I group this to get the following Python output

[(A,B), (B,C), (C, D), (D,E), (E,F), (F,G)]

So the values are grouped by the secound value but the order is preserved…

  • 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-14T09:09:08+00:00Added an answer on June 14, 2026 at 9:09 am

    Try using zip():

    zip(lst, lst[1:])
    

    Also, you shouldn’t use the name list, as you will override the built-in list type.

    Example:

    >>> lst = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
    >>> zip(lst, lst[1:])
    [('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'F'), ('F', 'G')]
    

    For a version that will work with generators or other one-pass iterables, you can use the pairwise recipe from the itertools docs:

    from itertools import tee, izip
    
    def pairwise(iterable):
        "s -> (s0,s1), (s1,s2), (s2, s3), ..."
        a, b = tee(iterable)
        next(b, None)
        return izip(a, b)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: iterate through pairs of items in python list I have a list
Possible Duplicate: reading lines 2 at a time In python we can iterate over
Possible Duplicate: how can I iterate through two lists in parallel in Python? i
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: Turning long fixed number to array Ruby Well, I have to iterate
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Possible Duplicate: How to enumerate an enum? Suppose that i have an enumeration like

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.