Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
making it be a list of it
#5
You should look into the module typing.
It has a ABC for Iterators.
An Iterator is iterable, but not all iterables are iterators...

from typing import Iterator


x = list(it) if isinstance(it, Iterator) else [it]
If you want to check if the object is in iterator, you can use the built in iter function:

def gen():
    yield 42


a = gen()
b = gen()
c = [1]
d = [2]

iter(a) is iter(a) # True, it's the same iterator
iter(a) is iter(b) # False, we have two different Iterators
iter(c) is iter(c) # False, two different Iterators. A list object is not an Iterator, but it's iterable.
iter(c) is iter(d) # False, two different lists and two different Iterators
The objects c and d are lists (sequences) and not Iterators. They are iterable.
A iterator just knows the state during iteration.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
making it be a list of it - by Skaperen - Mar-18-2018, 02:33 AM
RE: making it be a list of it - by Gribouillis - Mar-18-2018, 10:48 PM
RE: making it be a list of it - by DeaD_EyE - Mar-19-2018, 09:18 AM
RE: making it be a list of it - by wavic - Mar-19-2018, 08:54 AM
RE: making it be a list of it - by buran - Mar-19-2018, 09:11 AM
RE: making it be a list of it - by Gribouillis - Mar-19-2018, 09:47 AM
RE: making it be a list of it - by Skaperen - Mar-22-2018, 04:12 AM
RE: making it be a list of it - by buran - Mar-22-2018, 01:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Making a copy list in a function RuyCab 1 1,842 Jul-11-2021, 02:06 PM
Last Post: Yoriz
  making list in looping a dictionary glennford49 9 3,662 Jun-25-2020, 03:23 PM
Last Post: ndc85430
  making the code easier, list comprehension go127a 2 2,104 May-26-2019, 06:19 PM
Last Post: Gribouillis
  Making list empty after return in function dan789 10 5,866 Nov-24-2018, 11:54 PM
Last Post: micseydel
  Making a number list in a .txt file kwak86 13 6,282 Sep-16-2018, 09:54 PM
Last Post: kwak86

Forum Jump:

User Panel Messages

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