Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I need the last 9 numbers of a list and I'm sure there is a way to do it with slicing, but I can't seem to get it. I can get the first 9 like this:

num_list[0:9]

Any help would be great.

1 Answer

0 votes
by (106k points)

To get the last item of a list in Python you can use negative integers with the slicing operator for that. Here's an example using the python CLI interpreter:

>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 

>>> a 

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 

>>> a[-9:] 

[4, 5, 6, 7, 8, 9, 10, 11, 12]

the important line is a[-9:]

Related questions

0 votes
1 answer
0 votes
1 answer
+1 vote
2 answers
0 votes
1 answer
asked Oct 15, 2019 in Python by Sammy (47.6k points)

Browse Categories

...