Python Forum
making it be a list of it - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: making it be a list of it (/thread-9027.html)



making it be a list of it - Skaperen - Mar-18-2018

i am getting a value or reference from some place. i'll call it it. that place can be pretty much anywhere such as an element of a tuple. it may or may not be a list. if i am getting it from a set, it cannot be a list. but in this expression i need for it to be a list, even if the list is just one element having the original it as that element. doing list(it) won't work because it might not be an iterator.

i am looking for an existing way to make a non-iterator into a 1-list (actually, just need to iterate over it). having the non-iterator as the one element or if it is an iterator, either leave it as it is or convert it to another iterator form such as a list or tuple.

i can write a function to do this. or i can add extra lines of code and handle it that way. but, before i take that approach. i want to see if there is a nice way to do this inline with what comes with Python3 (e.g. nothing to install). if an idea can also be done in Python2 using the same code, that would be great, but not essential. it just needs to work in Python3.


RE: making it be a list of it - Gribouillis - Mar-18-2018

You can perhaps use
x = list(it) if hasattr(it, '__iter__') else [it,]



RE: making it be a list of it - wavic - Mar-19-2018

try:
    x = list(it)
except TypeError as e:
    print(e)



RE: making it be a list of it - buran - Mar-19-2018

@wavic
that is exactly what OP wants to resolve - with your code what will happen if it is as str?


RE: making it be a list of it - DeaD_EyE - Mar-19-2018

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.


RE: making it be a list of it - Gribouillis - Mar-19-2018

Also read the doc of collections.abc.Iterable.


RE: making it be a list of it - Skaperen - Mar-22-2018

i just need to be able to iterate over it as one thing and get that one thing that can't be iterated, if it makes sense to do so. as far as i know, anything i can iterate over makes sense. i can iterate over a string (.__iter__ is there). but maybe i want to treat a string as one thing that is not to be iterated over and, instead, is to be put in a 1-list.


RE: making it be a list of it - buran - Mar-22-2018

def make_list(it):
    return it if isinstance(it, (list, tuple)) else [it,]
    
print(make_list([1,2,3]))
print(make_list('some string'))
Output:
[1, 2, 3] ['some string']