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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:08:14+00:00 2026-06-16T00:08:14+00:00

Possible Duplicate: A Transpose/Unzip Function in Python I have a list of sequences, each

  • 0

Possible Duplicate:
A Transpose/Unzip Function in Python

I have a list of sequences, each sequence has two items. I would like to turn this into two lists.

catalog = [('abc', '123'), ('foo', '456'), ('bar', '789'), ('test', '1337')]

Right now I’m just doing this:

names = []
vals = []

for product in catalog:
        names.append(product[0])
        vals.append(product[1])

print (names)
print (vals)

Which outputs two lists, and works just fine:

['abc', 'foo', 'bar', 'test']
['123', '456', '789', '1337']

Is there a neater, more ‘pythonic’ way of doing this? Or should I stick with what I have? Any corrections or feedback on programming style is welcome, I’m new and trying to learn best practices.

  • 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-16T00:08:15+00:00Added an answer on June 16, 2026 at 12:08 am
    >>> catalog = [('abc', '123'), ('foo', '456'), ('bar', '789'), ('test', '1337')]
    >>> names, vals = zip(*catalog)
    >>> names
    ('abc', 'foo', 'bar', 'test')
    >>> vals
    ('123', '456', '789', '1337')
    

    The *catalog syntax here is called Unpacking Argument Lists, and zip(*catalog) translates into the call zip(catalog[0], catalog[1], catalog[2], ...).

    The zip() builtin function groups iterables by indices, so when you pass a bunch of two-element tuples as above, you get a two-element list of tuples where the first tuple contains the first element of each tuple from catalog, and the second tuple contains the second element from each tuple from catalog.

    In a quick timeit test the zip() version outperforms a looping approach when I tested with 1,000,000 pairs:

    In [1]: catalog = [(i, i+1) for i in range(1000000)]
    
    In [2]: def with_zip():
       ...:     return zip(*catalog)
       ...: 
    
    In [3]: def without_zip():
       ...:     names, vals = [], []
       ...:     for name, val in catalog:
       ...:         names.append(name)
       ...:         vals.append(val)
       ...:     return names, vals
       ...: 
    
    In [4]: %timeit with_zip()
    1 loops, best of 3: 176 ms per loop
    
    In [5]: %timeit without_zip()
    1 loops, best of 3: 250 ms per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Matrix Transpose in Python I have a matrix, say A = [[0,0],[1,1]]
Possible Duplicate: Trying to get tables next to each other horizontal I have two
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: C#/WPF: Toolkit DataGrid - Transpose rows and columns I would like to
Possible Duplicate: Get the size of a list in python? I need to be
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: Split A4 PDF page into two A5 and back again I have
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Can’t create handler inside thread that has not called Looper.prepare() inside AsyncTask

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.