Dylan M
Dylan M

Reputation: 57

How can I create multiple arrays from lines out of a file?

with open('filename') as f:
    list1 = f.read().splitlines()
    print list1

This gives me a comma-separated list with each line from the file like this: ['line1', 'line2', 'line3',...]

I now want to separate each line into it's own array like so: [ [line1], [line2], [line3]...] -How can I do this?

Upvotes: 0

Views: 113

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 118001

You can create a list within a list comprehension

with open('filename') as f:
    list1 = [[i] for i in f.read().splitlines()]
    print list1

Upvotes: 2

Related Questions