Matthew
Matthew

Reputation: 865

Convert text into a list of its lines

My output from a function are all values that are broken on separate lines, I would like to turn this into a list.

The Score
Leon the Professional
Iron Man

I want to turn this into a list like:

movies= ['The Score', 'Leon the Professional', 'Iron Man']

How can I go about doing this?

Upvotes: 1

Views: 203

Answers (2)

clwen
clwen

Reputation: 20909

Assuming you're reading lines from a file:

with open('lines.txt') as f:
    lines = f.readlines()
    output = []
    for line in lines:
        output.append(line.strip())

Upvotes: 0

jamylak
jamylak

Reputation: 133534

Assuming your input is a string.

>>> text = '''The Score
Leon the Professional
Iron Man'''
>>> text.splitlines()
['The Score', 'Leon the Professional', 'Iron Man']

More information on the splitlines() function.

Upvotes: 9

Related Questions