Python – Creates a list of strings when input does not separate python

Creates a list of strings when input does not separate python… here is a solution to the problem.

Creates a list of strings when input does not separate python

I

have this list online with 200 names on the list, which I saved in a text file.

John
Noah
William
James
Logan
Benjamin
...

I want them to be a list of strings ie

x=['John','Noah','William',...]

I

searched for similar issues but didn’t find what I really needed, thanks a lot for any help.

Solution

If the input is a file, you can do….

x = []
with open('file_name', 'r') as f:
    for line in f:
       x.append(line.strip())

Related Problems and Solutions