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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:41:46+00:00 2026-06-02T10:41:46+00:00

In Python 3 I can do the following (see also PEP3132 on Extended Iterable

  • 0

In Python 3 I can do the following (see also PEP3132 on Extended Iterable Unpacking):

a, *b = (1, 2, 3)
# a = 1; b = (2, 3)

What can I do to achieve the same similarly elegant in Python 2.x?


I know that I could use single element access and slicing operations, but I wonder if there is a more pythonic way. My code so far:

a, b = (1, 2, 3)[0], (1, 2, 3)[1:]
# a = 1; b = (2, 3)
  • 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-02T10:41:54+00:00Added an answer on June 2, 2026 at 10:41 am

    I found out that the related PEP3132 gives some examples for Python 2.x as well:

    Many algorithms require splitting a sequence in a "first, rest" pair:

    first, rest = seq[0], seq[1:]
    

    […]

    Also, if the right-hand value is not a list, but an iterable, it has to be converted to a list before being able to do slicing; to avoid creating this temporary list, one has to resort to

    it = iter(seq)
    first = it.next()
    rest = list(it)
    

    Other approaches given in the answers to this question:

    Function Argument List Unpacking Approach

    requires an extra function definition/call:

    def unpack(first, *rest): 
      return first, rest
    first, rest = unpack( *seq )
    

    I wonder why it is implemented in unpacking function argument lists but not for normal tuple unpacking.

    Generator Approach

    Credits. Also requires a custom function implementation. Is a little more flexible concerning the number of first variables.

    def unpack_nfirst(seq, nfirst):
      it = iter(seq)
      for x in xrange(nfirst):
        yield next(it, None)
      yield tuple(it)
    first, rest = unpack_nfirst(seq, 1)
    

    The most pythonic would probably be the ones mentioned in the PEP above, I guess?

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

Sidebar

Related Questions

If you look at the following line of python code: bpy.ops.object.particle_system_add({object:bpy.data.objects[2]}) you see that
I see that we can try Flask (the python micro framework from the browser,
So I know in python everything is an 'object' meaning that it can be
In python, I can do the following, to conditionally use a 2nd list if
Possible Duplicate: Hex to binary in ruby In Python, I can do the following:
How can i match the following dates with python regular expression in one statement,
I know python can be run on GAE what is different erlang and python
When using Python is it possible that a dict can have a value that
I am not that familiar with PyGTK. Please see the following code. Is it
So, Python functions can return multiple values. It struck me that it would be

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.