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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:08:39+00:00 2026-06-11T12:08:39+00:00

I had some code that contained zip(*G)[0] (and elsewhere, zip(*G)[1] , with a different

  • 0

I had some code that contained zip(*G)[0] (and elsewhere, zip(*G)[1], with a different G). G is a list of tuples. What this does is return a list of the first (or generally, for zip(*G)[n], the n-1th) element of each tuple in G as a tuple. For example,

>>> G = [(1, 2, 3), ('a', 'b', 'c'), ('you', 'and', 'me')]
>>> zip(*G)[0]
(1, 'a', 'you')
>>> zip(*G)[1]
(2, 'b', 'and')

This is pretty clever and all, but the problem is that it doesn’t work in Python 3, because zip is an iterator there. Furthermore, 2to3 isn’t smart enough to fix it. So the obvious solution is to use list(zip(*G))[0], but that got me thinking: there is probably a more efficient way to do this. There is no need to create all the tuples that zip creates. I just need the nth element of each tuple in G.

Is there are more efficient, but equally compact way to do this? I’m OK with anything from the standard library. In my use case, each tuple in G will be at least length n, so there is no need to worry about the case of zip stopping at the smallest length tuple (i.e., zip(*G)[n] will always be defined).

If not, I guess I’ll just stick with wrapping the zip in list().

(P.S., I know this is unnecessary optimization. I’m just curious is all)

UPDATE:

In case anyone cares, I went with the t0, t1, t2 = zip(*G) option. First, this lets me give meaningful names to the data. My G actually consists of length 2 tuples (representing numerators and denominators). A list comprehension would only be marginally more readable than the zip, but this way is much better (and since in most cases the zip was list I was iterating through in a list comprehension, this makes things flatter).

Second, as noted by @thewolf and @Sven Marnach’s excellent answers, this way is faster for smaller lists. My G is actually not large in most cases (and if it is large, then this definitely won’t be the bottleneck of the code!).

But there were more ways to do this than I expected, including the new a, *b, c = G feature of Python 3 I didn’t even know about.

  • 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-11T12:08:40+00:00Added an answer on June 11, 2026 at 12:08 pm

    At least the fastest way in Python 2.7 is

    t0,t1,t2=zip(*G) for SMALLER lists and [x[0] for x in G] in general
    

    Here is the test:

    from operator import itemgetter
    
    G = [(1, 2, 3), ('a', 'b', 'c'), ('you', 'and', 'me')]
    
    def f1():
       return tuple(x[0] for x in G)
    
    def f2():
       return tuple(map(itemgetter(0), G))
    
    def f3():
        return tuple(x for x, y, z in G)     
    
    def f4():
        return tuple(list(zip(*G))[0])
    
    def f5():
        t0,*the_rest=zip(*G)
        return t0
    
    def f6():
        t0,t1,t2=zip(*G)
        return t0                
    
    cmpthese.cmpthese([f1,f2,f3,f4,f5,f6],c=100000) 
    

    Results:

        rate/sec     f4     f5     f1     f2     f3     f6
    f4   494,220     -- -21.9% -24.1% -24.3% -26.6% -67.6%
    f5   632,623  28.0%     --  -2.9%  -3.0%  -6.0% -58.6%
    f1   651,190  31.8%   2.9%     --  -0.2%  -3.2% -57.3%
    f2   652,457  32.0%   3.1%   0.2%     --  -3.0% -57.3%
    f3   672,907  36.2%   6.4%   3.3%   3.1%     -- -55.9%
    f6 1,526,645 208.9% 141.3% 134.4% 134.0% 126.9%     --
    

    If you don’t care if the result is a list, a list comprehension if faster.

    Here is a more extended benchmark with variable list sizes:

    from operator import itemgetter
    import time
    import timeit 
    import matplotlib.pyplot as plt
    
    def f1():
       return [x[0] for x in G]
    
    def f1t():
       return tuple([x[0] for x in G])
    
    def f2():
       return tuple([x for x in map(itemgetter(0), G)])
    
    def f3():
        return tuple([x for x, y, z in G])    
    
    def f4():
        return tuple(list(zip(*G))[0])
    
    def f6():
        t0,t1,t2=zip(*G)
        return t0     
    
    n=100    
    r=(5,35)
    results={f1:[],f1t:[],f2:[],f3:[],f4:[],f6:[]}    
    for c in range(*r):
        G=[range(3) for i in range(c)] 
        for f in results.keys():
            t=timeit.timeit(f,number=n)
            results[f].append(float(n)/t)
    
    for f,res in sorted(results.items(),key=itemgetter(1),reverse=True):
        if f.__name__ in ['f6','f1','f1t']:
            plt.plot(res, label=f.__name__,linewidth=2.5)
        else:    
            plt.plot(res, label=f.__name__,linewidth=.5)
    
    plt.ylabel('rate/sec')
    plt.xlabel('data size => {}'.format(r))  
    plt.legend(loc='upper right')
    plt.show()
    

    Which produces this plot for smaller data sizes (5 to 35):

    smaller

    And this output for larger ranges (25 to 250):

    larger

    You can see that f1, a list comprehension is fastest. f6 and f1t trading places as the fastest to return a tuple.

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

Sidebar

Related Questions

A co-worker asked about some code like this that originally had templates in it.
I had some code that constructed an object: function gridObjConst(id, itemName, itemPrice, itemListPrice, width,
I had some code that ran commands through Runtime.getRuntime.exec(String) , and it worked on
I had some decryption code (using wincrypt.h ) that lived within my FileReader.cpp class.
This question has two parts. Part 1. Yesterday I had some code which would
I had to de-compile some code and I don't know what this syntax is?
I saw some code that had the controls in a flex box set to
I had to convert some code (which deals with building a VBO of data
I'm writing some code here, and I'm having a had time. I have a
I'm having a problem with some code I've written. I've had to anonymize it,

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.