write unicode objects from loop to list in Python

Polly

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'
brianpck

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to generate a single list of objects from a for loop in python?

How to remove all objects in a list from a for loop with python?

Recursive loop through nested array of objects to write nested list

Append data to component with loop from objects list

Django how to save objects in a for loop from list

How to write and loop a list on a column in python dataframe?

How to write an indexed loop over a list in Python?

For Loop for list of Objects with use Multithread in Python

In Python, how do I write a loop to remove from character # n to a specific character (:) in parts of a list that match a condition?

Read and write Unicode character from Json file using Python

python add objects from list into another list

Extract list of attributes from list of objects in python

Filtering List of objects int loop by id from outer loop

Return a list from a for loop python

How to convert from unicode list to normal list in python/django

How can Python get a list from XML such as Unicode to a list?

How to loop lists of list obtained from python-docx where each list is a table and write the tables into a seperate worksheets

Extract attributes from objects in a list and write them into a dataframe

python write data from the list to a CSV a file

write to xml file from list python

Unable to write values from list to csv python

Write to a file from a list with integer Python

Write recursive data in excel from python list

How to access multiple image objects in a loop, from outside of the loop in python?

write unicode data to mssql with python?

How to write a loop that adds all the numbers from the list into a variable

generate list of objects with for loop

Importing objects from another python file as a list

Delete an object from a list of objects in python