user2971812
user2971812

Reputation: 429

Python - get last item in a list, using a file

I have a file:

enter image description here

I split the lines so I get this:

enter image description here

But I'm only interested in the last item in each of those lists, so 77, 77, 6 etc. Is there a way to obtain them?

Upvotes: 0

Views: 715

Answers (3)

Hackaholic
Hackaholic

Reputation: 19743

if you are working with big file:

>>> with open('yourfile') as f:
...     last_items = [ int(x.strip().split()[-1]) for i,x in enumerate(f) if i> 1 ]
... 

Upvotes: 0

Logicrat
Logicrat

Reputation: 4468

For each item, take its [-1] element. Index [-1] refers to the last element of an array, [-2] to the next-to-last, etc.

Upvotes: 0

Vedaad Shakib
Vedaad Shakib

Reputation: 729

Assuming you already have that array,

lastItems = [int(i[-1]) for i in yourList[2:]]

Upvotes: 2

Related Questions