I have a list of integers, for example:
Mylist = [3, 6, 17, 55]
I have a Myfile.txt
file containing 100s of lines.
Now I have to extract the lines which are present in Mylist
from Myfile.txt
and store them in another list.
I have a list of integers, for example:
Mylist = [3, 6, 17, 55]
I have a Myfile.txt
file containing 100s of lines.
Now I have to extract the lines which are present in Mylist
from Myfile.txt
and store them in another list.
Here's a solution making use of list comprehension
with open('Myfile.txt','r') as f:
data = f.read().split('\n')
NewList = [data[x] for x in Mylist]