Python Forum
For loop with 2 elements. No zip
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loop with 2 elements. No zip
#5
If you wanted to make a single sequence from the two, you could use chain from the itertools module, e.g.

>>> import itertools
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> itertools.chain(x, y)
<itertools.chain object at 0x7f38338dfb90>
>>> list(itertools.chain(x, y))
[1, 2, 3, 4, 5, 6]
chain returns an iterator (basically a form of lazy sequence), so I pass that to list to force evaluation of all the items (I could have printed or something too, of course).
Reply


Messages In This Thread
For loop with 2 elements. No zip - by amassotti - May-03-2020, 07:48 AM
RE: For loop with 2 elements. No zip - by buran - May-03-2020, 08:00 AM
RE: For loop with 2 elements. No zip - by buran - May-03-2020, 08:01 AM
RE: For loop with 2 elements. No zip - by ndc85430 - May-03-2020, 08:02 AM
RE: For loop with 2 elements. No zip - by bowlofred - May-03-2020, 08:08 AM
RE: For loop with 2 elements. No zip - by amassotti - May-03-2020, 09:12 AM
RE: For loop with 2 elements. No zip - by buran - May-03-2020, 09:23 AM
RE: For loop with 2 elements. No zip - by amassotti - May-03-2020, 10:34 AM
RE: For loop with 2 elements. No zip - by buran - May-03-2020, 10:49 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,226 May-17-2022, 11:38 AM
Last Post: Larz60+
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,643 Feb-10-2021, 04:53 PM
Last Post: rpalmer
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 2,995 Dec-02-2020, 07:50 AM
Last Post: Gilush
  How do I loop through a list and delete numerical elements that are 1 lower/higher? neko 4 4,332 Sep-05-2017, 02:25 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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