Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iterating a list of ranges
#1
i have multiple ranges i wish to iterate over. but the obvious solution (2 nested levels of iteration) is not available because the iteration code, which may be in a function, expects one iterator and iterates over that with just a single level (this code cannot be changed). producing a list from all the ranges and passing the list as the one iterator would work, but i want to avoid that in the list is so large it causes swapping. do i need to make a generator do this or is there a better way?

in one use case, the last range is effectively infinite, intend to make that one level iteration run "forever".
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
You can use itertools.chain() or itertools.chain.from_iterable()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,463 May-14-2023, 04:29 PM
Last Post: Winfried
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,800 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Delete list while iterating shantanu97 1 1,897 Jun-06-2021, 11:59 AM
Last Post: Yoriz
  Dictionary with ranges that have a float step value Irv1n 2 2,122 Apr-21-2021, 09:04 PM
Last Post: Yoriz
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,284 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138
  python3: iterating through list not working wardancer84 3 2,367 Jul-08-2020, 04:30 PM
Last Post: DPaul
  Two operations in two ranges salwa17 3 2,145 Jun-22-2020, 04:15 PM
Last Post: perfringo
  Indexing problem while iterating list and subtracting lbtdne 2 2,138 May-14-2020, 10:19 PM
Last Post: deanhystad
  iterating a span of a list Skaperen 5 3,064 Dec-29-2019, 08:15 PM
Last Post: Skaperen
  Iterating through a list of strings Ash_Ren 1 2,091 Nov-22-2019, 08:30 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020