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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:18:33+00:00 2026-06-01T05:18:33+00:00

I am wondering about the use of == when comparing two generators For example:

  • 0

I am wondering about the use of == when comparing two generators

For example:

x = ['1','2','3','4','5']

gen_1 = (int(ele) for ele in x)
gen_2 = (int(ele) for ele in x)

gen_1 and gen_2 are the same for all practical purposes, and yet when I compare them:

>>> gen_1 == gen_2
False

My guess here is that == here is treated like is normally is, and since gen_1 and gen_2 are located in different places in memory:

>>> gen_1
<generator object <genexpr> at 0x01E8BAA8>
>>> gen_2
<generator object <genexpr> at 0x01EEE4B8>

their comparison evaluates to False. Am I right on this guess? And any other insight is welcome.

And btw, I do know how to compare two generators:

>>> all(a == b for a,b in zip(gen_1, gen_2))
True

or even

>>> list(gen_1) == list(gen_2)
True

But if there is a better way, I’d love to know.

  • 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-01T05:18:34+00:00Added an answer on June 1, 2026 at 5:18 am

    You are right with your guess – the fallback for comparison of types that don’t define == is comparison based on object identity.

    A better way to compare the values they generate would be

    from itertools import zip_longest, tee
    sentinel = object()
    all(a == b for a, b in zip_longest(gen_1, gen_2, fillvalue=sentinel))
    

    (For Python 2.x use izip_longest instead of zip_longest)

    This can actually short-circuit without necessarily having to look at all values. As pointed out by larsmans in the comments, we can’t use zip() here since it might give wrong results if the generators produce a different number of elements – zip() will stop on the shortest iterator. We use a newly created object instance as fill value for zip_longest(), since object instances compare unequal to any sane value that could appear in one of the generators (including other object instances).

    Note that there is no way to compare generators without changing their state. You could store the items that were consumed if you need them later on:

    gen_1, gen_1_teed = tee(gen_1)
    gen_2, gen_2_teed = tee(gen_2)
    all(a == b for a, b in zip_longest(gen_1, gen_2, fillvalue=sentinel))
    

    This will give leave the state of gen_1 and gen_2 essentially unchanged. All values consumed by all() are stored inside the tee object.

    At that point, you might ask yourself if it is really worth it to use lazy generators for the application at hand — it might be better to simply convert them to lists and work with the lists instead.

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

Sidebar

Related Questions

I'm wondering about the practical use of #undef in C. I'm working through K&R,
I'm wondering about computational efficiency. I'm going to use Java in this example, but
I'm wondering about instances when it makes sent to use #define and #if statements.
I am wondering about the relationship between .NET ClassLoader and Assembly I use the
I was wondering what language to use when talking about a function that takes
I use log4net in just about every code project. I was wondering what other
Just wondering about the performance impact of copying very large php variables. For example
I was wondering about the use of the .NET entity framework with an embedded
im wondering about how to use WCF service instead of sockets .. to send
I am wondering how I would go about externalizing a proc (example below) so

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.