Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
making it be a list of it
#1
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.
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
You can perhaps use
x = list(it) if hasattr(it, '__iter__') else [it,]
Reply
#3
try:
    x = list(it)
except TypeError as e:
    print(e)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
@wavic
that is exactly what OP wants to resolve - with your code what will happen if it is as str?
Reply
#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
#6
Also read the doc of collections.abc.Iterable.
Reply
#7
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
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']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Making a copy list in a function RuyCab 1 1,793 Jul-11-2021, 02:06 PM
Last Post: Yoriz
  making list in looping a dictionary glennford49 9 3,560 Jun-25-2020, 03:23 PM
Last Post: ndc85430
  making the code easier, list comprehension go127a 2 2,060 May-26-2019, 06:19 PM
Last Post: Gribouillis
  Making list empty after return in function dan789 10 5,751 Nov-24-2018, 11:54 PM
Last Post: micseydel
  Making a number list in a .txt file kwak86 13 6,097 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