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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:02:57+00:00 2026-06-05T16:02:57+00:00

I want to toggle between two values in Python, that is, between 0 and

  • 0

I want to toggle between two values in Python, that is, between 0 and 1.

For example, when I run a function the first time, it yields the number 0. Next time, it yields 1. Third time it’s back to zero, and so on.

Sorry if this doesn’t make sense, but does anyone know a way to do this?

  • 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-05T16:02:59+00:00Added an answer on June 5, 2026 at 4:02 pm

    Use itertools.cycle():

    from itertools import cycle
    myIterator = cycle(range(2))
    
    myIterator.next()   # or next(myIterator) which works in Python 3.x. Yields 0
    myIterator.next()   # or next(myIterator) which works in Python 3.x. Yields 1
    # etc.
    

    Note that if you need a more complicated cycle than [0, 1], this solution becomes much more attractive than the other ones posted here…

    from itertools import cycle
    mySmallSquareIterator = cycle(i*i for i in range(10))
    # Will yield 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 0, 1, 4, ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to toggle between two divs. For example, onload, I want the
I have a div that I want to toggle between displaying or not when
I want to toggle element that id's pg when click link. header: <title>Example</title> <script
I am trying to create a function that will toggle between a play and
I want a layout such that user can toggle between listview and mapview and
sir i want to know that how could i navigate/toggle between the different exe
Have two UIBarButtonItems want to make it as one UIBarButtonItem and toggle between them
What I want is an animated div which will toggle height between 300px and
I want to toggle between CSS so when a user clicks the button (
How can I make a loop to toggle between these two functions continuously? Moreover,

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.