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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:08:21+00:00 2026-06-17T22:08:21+00:00

Is there a big difference in speed in these two code fragments? 1. x

  • 0

Is there a big difference in speed in these two code fragments?

1.

x = set( i for i in data )

versus:

2.

x = set( [ i for i in data ] )

I’ve seen people recommending set() instead of set([]); is this just a matter of style?

  • 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-17T22:08:22+00:00Added an answer on June 17, 2026 at 10:08 pm

    The form

    x = set(i for i in data)
    

    is shorthand for:

    x = set((i for i in data))
    

    This creates a generator expression which evaluates lazily. Compared to:

    x = set([i for i in data])
    

    which creates an entire list before passing it to set


    From a performance standpoint, generator expressions allow for short-circuiting in certain functions (all and any come to mind) and takes less memory as you don’t need to store the extra list — In some cases this can be very significant.

    If you actually are going to iterate over the entire iterable data, and memory isn’t a problem for you, I’ve found that typically the list-comprehension is slightly faster then the equivalent generator expression*.

    temp $ python -m timeit 'set(i for i in "xyzzfoobarbaz")'
    100000 loops, best of 3: 3.55 usec per loop
    temp $ python -m timeit 'set([i for i in "xyzzfoobarbaz"])'
    100000 loops, best of 3: 3.42 usec per loop
    

    Note that if you’re curious about speed — Your fastest bet will probably be just:

    x = set(data)
    

    proof:

    temp $ python -m timeit 'set("xyzzfoobarbaz")'
    1000000 loops, best of 3: 1.83 usec per loop
    

    *Cpython only — I don’t know how Jython or pypy optimize this stuff.

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

Sidebar

Related Questions

Is there really a big difference between Eclipse 3.2 and 3.4? I am currently
I'm wondering if there is a big difference when loading graphics made in pure
I have been running around here and there on NoSQL big data storage technologies.
Possible Duplicate: Synchronization vs Lock I am wondering is there big difference in using
Is there a big difference (in terms of performance, browser implementation availability, server load
There is a big difference - talking about transfer rate, object serialization - to
Is there a big performance difference between: Process A writing to a temp file,
Is the Linux kernel preemptive or not? There is a big difference between Understanding
There is no big difference between those functions except the syntax: $('.target').append('text'); $('text').appendTo('.target'); As
When using jQuery events such as click, mouseover and so, are there big differences

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.