Python Forum
is a str object a valid iterator?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is a str object a valid iterator?
#1
is a str object a valid iterator?
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
In [1]: next('Foo')                                                                                                         
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-60a92f7bb96c> in <module>
----> 1 next('Foo')

TypeError: 'str' object is not an iterator
No, it's an iterable, but not an iterator.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
(Jan-27-2020, 02:13 AM)DeaD_EyE Wrote: No, it's an iterable, but not an iterator.

..and to make iterator from iterable there is built-in function iter():

>>> next(iter('42'))                                                                                                                    
'4'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Yes, but you should have a reference to the iterator:

greeting = "Hello World"
iterator = iter(greeting)

print(next(iterator))
print(next(iterator))
If you do next(iter('42')) two times, you'll get 4 two times.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(Jan-27-2020, 09:18 AM)DeaD_EyE Wrote: If you do next(iter('42')) two times, you'll get 4 two times.

Yes, you are absolutely right. There is ambiguity in my post, it was too much about next() and not about practical application.

Just to add one more tidbit: for-loop creates iterator from iterable 'behind the scenes'.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
Yes, this is one fact, that many people don't know.


for element in "abc":
    pass
is the same like

for element in iter("abc"):
    pass
The first one is implicit, the second explicit.
I use "explicit" iterators often in while-loops.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
(Jan-27-2020, 02:13 AM)DeaD_EyE Wrote: No, it's an iterable, but not an iterator.
my bad; i should have said "iterable".

is it treated as an iterable by itertools.chain?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Script getting reindexing only valid error cubangt 1 795 Dec-07-2023, 04:06 PM
Last Post: cubangt
Question Use function, retry until valid Ashcora 8 1,405 Jan-06-2023, 10:14 AM
Last Post: Ashcora
  prime numbers with iterator and generator cametan_001 8 1,771 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  resetting an iterator to full Skaperen 7 6,808 Feb-20-2022, 11:11 PM
Last Post: Skaperen
  popping an iterator Skaperen 11 3,600 Oct-03-2021, 05:08 PM
Last Post: Skaperen
  checking for valid hexadecimal digits Skaperen 3 6,265 Sep-02-2021, 07:22 AM
Last Post: buran
  q re glob.iglob iterator and close jimr 2 2,178 Aug-23-2021, 10:14 PM
Last Post: perfringo
  Problem with an iterator grimm1111 9 4,217 Feb-06-2021, 09:22 PM
Last Post: grimm1111
  Multi-class iterator Pedroski55 2 2,335 Jan-02-2021, 12:29 AM
Last Post: Pedroski55
  Limiting valid values for a variable in a class WJSwan 5 3,510 Jul-06-2020, 07:17 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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