Python Forum
Why not use len(alist) in an iterator?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why not use len(alist) in an iterator?
#6
parwise as used bye bowlofred is a ideal soltion for this.
So look into itertools and eg more-itertools they have lot of stuff that simplify looping and index manipulation.

Can also do like this with build in zip,but like pairwise from itertools better.
from random import randint

nums = [6, 2, 8, 4, 3, 3, 1, 7, 9, 7 ,7, 1]
for current, next_one in zip(nums, nums[1:]):
    if current == next_one:
        print(f'found 2 equal numbers in sequence {current} and {next_one}')
Pedroski55 likes this post
Reply


Messages In This Thread
RE: Why not use len(alist) in an iterator? - by snippsat - Jun-27-2024, 02:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  prime numbers with iterator and generator cametan_001 8 3,591 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  resetting an iterator to full Skaperen 7 11,040 Feb-20-2022, 11:11 PM
Last Post: Skaperen
  popping an iterator Skaperen 11 5,551 Oct-03-2021, 05:08 PM
Last Post: Skaperen
  q re glob.iglob iterator and close jimr 2 3,126 Aug-23-2021, 10:14 PM
Last Post: perfringo
  Problem with an iterator grimm1111 9 6,039 Feb-06-2021, 09:22 PM
Last Post: grimm1111
  Multi-class iterator Pedroski55 2 3,068 Jan-02-2021, 12:29 AM
Last Post: Pedroski55
  is a str object a valid iterator? Skaperen 6 7,299 Jan-27-2020, 08:44 PM
Last Post: Skaperen
  discard one from an iterator Skaperen 1 2,583 Dec-29-2019, 11:02 PM
Last Post: ichabod801
  looking for a sprcil iterator Skaperen 7 4,725 Jun-13-2019, 01:40 AM
Last Post: Clunk_Head
  last pass of for x in iterator: Skaperen 13 7,782 May-20-2019, 10:05 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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