I have a loop that returns me unicode objects:
for i in X:
print i
Output:
A
B
C
...
Z
How can I make a list of these objects to get the folloving?
['A', 'B', ..., 'Z']
If they were numbers, i'd do
for i in X:
y=[]
i.append(y)
but here i get the error
AttributeError: 'unicode' object has no attribute 'append'
If you are looping through a variable, you already know it is an iterable, such as a generator or list. First, you need to tell us the type of the x
variable in your statement.
If your expected output is a list and x
is not one, simply call the list
function:
new_list = list(x)
If it's necessary that you build the list in a loop for some reason, you should call the append
function on a previously created list--not an item, as you do in your example:
new_list = []
for i in x:
new_list.append(i)
print (new_list)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments