Vic Podestà
Vic Podestà

Reputation:

One liner to replicate lines coming from a file (Python)

I have a regular list comprehension to load all lines of a file in a list

f = open('file')

try:
    self._raw = [L.rstrip('\n') for L in f]
finally:
    f.close()

Now I'd like to insert in the list each line 'n' times on the fly. How to do it inside the list comprehension ?

Tnx

Upvotes: 1

Views: 360

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 882023

self._raw = [L.rstrip('\n') for L in f for _ in xrange(n)]

Upvotes: 6

Related Questions