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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:42:39+00:00 2026-05-22T11:42:39+00:00

I just extracted some data from a list using python but think it’s overcomplicated

  • 0

I just extracted some data from a list using python but think it’s overcomplicated and unpythonic and there’s probably a much better way to do this. I’m actually pretty sure I saw this somewhere in the standard library docs but my brain refuses to tell me where.

So here it goes:

Input:

x = range(8) # any even sequence

Output:

[[0, 1], [2, 3], [4, 5], [6, 7]]

My take:

[ [x[i], x[i+1]] for i in range(len(x))[::2] ]
  • 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-22T11:42:40+00:00Added an answer on May 22, 2026 at 11:42 am

    Tuples?

    In Python 2.n

    >>> zip(*2*[iter(x)])
    [(0, 1), (2, 3), (4, 5), (6, 7)]
    

    In Python 3.n

    zip() behaves slightly differently…

    >> zip(*2*[iter(x)])
    <zip object at 0x285c582c>
    >>> list(zip(*2*[iter(x)])])
    [(0, 1), (2, 3), (4, 5), (6, 7)]
    

    Lists?

    The implementation is the same in Python 2 and 3…

    >>> [[i,j] for i,j in zip(*2*[iter(x)])]
    [[0, 1], [2, 3], [4, 5], [6, 7]]
    

    Or, alternatively:

    >>> [list(t) for t in zip(*2*[iter(x)])]
    [[0, 1], [2, 3], [4, 5], [6, 7]]
    

    The latter is more useful if you want to split into lists of 3 or more elements, without spelling it out, such as:

    >>> [list(t) for t in zip(*4*[iter(x)])]
    [[0, 1, 2, 3], [4, 5, 6, 7]]
    

    If zip(*2*[iter(x)]) looks a little odd to you (and it did to me the first time I saw it!), take a look at How does zip(*[iter(s)]*n) work in Python?.

    See also this pairwise implementation, which I think is pretty neat.

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

Sidebar

Related Questions

I have an application that extracts data from some xml that is stored in
I extracted year, month, day, hour, minute, second, and millisecond data from human readable
This data comes from an Oracle database and is extracted to flatfiles in encoding
I'm trying to extract data from a data file that's tab-delimited (in some parts),
I'm building a process which extracts data from 6 csv-style files and two poorly
My customer doesn't want a database but would prefer to update their data in
The two posts below are great examples of different approaches of extracting data from
If I have the following nvarchar variable - BTA200, how can I extract just
I just want a very handy way to extract the numbers out of a
Just what the title says, I need to change the password for an existing

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.