09-27-2020, 11:36 PM
How to Get the Last Element of a Python List?
Problem: Given a list. How to access the last element of this list?
Example: You have the list and you want to get the last element .
Quick solution: Use negative indexing-1.
To access the last element of a Python list, use the indexing notation with negative index which points to the last list element. To access the second-, third-, and fourth-last elements, use the indices , , and . To access the last elements of a list, use slicing with negative stop index and negative step size .
Method 1: Access the Last Element with Negative Indexing -1
To bring everybody on the same page, let me quickly explain indices in Python by example. Suppose, you have list. The indices are simply the positions of the characters of this string.
(Positive) Index |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7
Element |
‘u’ |
‘n’ |
‘i’ |
‘v’ |
‘e’ |
‘r’ |
‘s’ |
‘e’
Negative Index |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1
Positive Index: The first character has index , the second character has index, and the -th character has index .
Negative Index: The last character has index, the second last character has index , and the -th last character has index .
Now, you can understand how to access the last element of the list:
But how to access the second-last element? Just use index -2!
Method 2: Access the n Last Elements with Slicing
But what if you want to access the last elements? The answer is slicing.
The default slicing operation accesses all elements between (included) and (excluded) indices, using the given size over the list. For example, the slicing operation would start with the first element and end with the third element (included), but taking only every second element due to the step size of —effectively skipping the second element .
You can use slicing with negative and indices and with negative stop size to slice from the right to the left. To access the last elements in the slice, you’d therefore use the following code:
There are different points to consider in the code:
Try this yourself in our interactive code shell:
https://repl.it/@finxter/FailingVagueAgents?lite=true
Exercise: What happens if the list has less than n characters?
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!
The post How to Get the Last Element of a Python List? first appeared on Finxter.
Problem: Given a list. How to access the last element of this list?
Example: You have the list
Code:
['Alice', 'Bob', 'Liz']Code:
'Liz'Quick solution: Use negative indexing-1.
Code:
friends = ['Alice', 'Bob', 'Liz']
print(friends[-1])
# LizTo access the last element of a Python list, use the indexing notation
Code:
list[-1]Code:
-1Code:
-2Code:
-3Code:
-4Code:
nCode:
list[:-n-1:-1]Code:
-nCode:
-1Method 1: Access the Last Element with Negative Indexing -1
To bring everybody on the same page, let me quickly explain indices in Python by example. Suppose, you have list
Code:
['u', 'n', 'i', 'v', 'e', 'r', 's', 'e'](Positive) Index |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7
Element |
‘u’ |
‘n’ |
‘i’ |
‘v’ |
‘e’ |
‘r’ |
‘s’ |
‘e’
Negative Index |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1
Positive Index: The first character has index , the second character has index
Code:
1Code:
iCode:
i-1Negative Index: The last character has index
Code:
-1Code:
-2Code:
iCode:
-iNow, you can understand how to access the last element of the list:
Code:
friends = ['Alice', 'Bob', 'Liz']
print(friends[-1])
# LizBut how to access the second-last element? Just use index -2!
Code:
friends = ['Alice', 'Bob', 'Liz']
print(friends[-2])
# BobBut what if you want to access the
Code:
nThe default slicing operation
Code:
list[start:stop:step]Code:
startCode:
stopCode:
stepCode:
friends[0:3:2]Code:
'Alice'Code:
'Liz'Code:
2Code:
'Bob'You can use slicing with negative
Code:
startCode:
stopCode:
nCode:
universe = ['u', 'n', 'i', 'v', 'e', 'r', 's', 'e'] # Access the n=4 last element from the list:
n = 4
print(universe[:-n-1:-1])
# ['e', 's', 'r', 'e']There are different points to consider in the code:
- You use a negative step size -1 which means that you slice from the right to the left.
- If you don’t provide a value for ,Code:
start, orCode:stopindices, Python takes the default ones. For example, we don’t provide theCode:stepindex and perform negative slicing so Python starts from the last elementCode:start.Code:'e'
- You want to get the last elements. TheCode:
n-th last element has indexCode:n. But as the stop index is never included in the slice, we need to slice one step further to the left—to the element with indexCode:-nto include the element with indexCode:-n-1.Code:-n
Try this yourself in our interactive code shell:
https://repl.it/@finxter/FailingVagueAgents?lite=true
Exercise: What happens if the list has less than n characters?
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!
The post How to Get the Last Element of a Python List? first appeared on Finxter.

