• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
[Tut] How to Get the Last Element of a Python List?

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 `['Alice', 'Bob', 'Liz']` and you want to get the last element `'Liz'`.

Quick solution: Use negative indexing -1.

```friends = ['Alice', 'Bob', 'Liz']
print(friends[-1])
# Liz```

To access the last element of a Python list, use the indexing notation `list[-1]` with negative index `-1` which points to the last list element. To access the second-, third-, and fourth-last elements, use the indices `-2`, `-3`, and `-4`. To access the `n` last elements of a list, use slicing `list[:-n-1:-1]` with negative stop index `-n` and negative step size `-1`.

## 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 `['u', 'n', 'i', 'v', 'e', 'r', 's', 'e']`. The indices are simply the positions of the characters of this string.

Positive Index: The first character has index `0`, the second character has index `1`, and the `i`-th character has index `i-1`.

Negative Index: The last character has index `-1`, the second last character has index `-2`, and the `i`-th last character has index `-i`.

Now, you can understand how to access the last element of the list:

```friends = ['Alice', 'Bob', 'Liz']
print(friends[-1])
# Liz```

But how to access the second-last element? Just use index -2!

```friends = ['Alice', 'Bob', 'Liz']
print(friends[-2])
# Bob```

## Method 2: Access the n Last Elements with Slicing

But what if you want to access the `n` last elements? The answer is slicing.

The default slicing operation `list[start:stop:step]` accesses all elements between `start` (included) and `stop` (excluded) indices, using the given `step` size over the list. For example, the slicing operation `friends[0:3:2]` would start with the first element `'Alice'` and end with the third element `'Liz'` (included), but taking only every second element due to the step size of `2`—effectively skipping the second element `'Bob'`.

You can use slicing with negative `start` and `stop` indices and with negative stop size to slice from the right to the left. To access the `n` last elements in the slice, you’d therefore use the following code:

```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 `start`, `stop`, or `step` indices, Python takes the default ones. For example, we don’t provide the `start` index and perform negative slicing so Python starts from the last element `'e'`.
• You want to get the `n` last elements. The `n`-th last element has index `-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 index `-n-1` to include the element with index `-n`.

Try this yourself in our interactive code shell:

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.

https://www.sickgaming.net/blog/2020/09/...thon-list/

 Possibly Related Threads… Thread Author Replies Views Last Post [Tut] Python’s NameError: name ‘xxx’ is not defined — How to Fix This Stupid Bug? xSicKxBot 0 8 10-16-2020, 10:30 PM Last Post: xSicKxBot [Tut] Python IndentationError: unexpected indent (How to Fix This Stupid Bug) xSicKxBot 0 15 10-10-2020, 08:24 PM Last Post: xSicKxBot [Tut] Yield Keyword in Python – A Simple Illustrated Guide xSicKxBot 0 16 10-09-2020, 06:13 PM Last Post: xSicKxBot [Tut] Python Reverse List with Slicing — An Illustrated Guide xSicKxBot 0 28 10-02-2020, 04:24 AM Last Post: xSicKxBot [Tut] How to Remove Duplicates From a Python List While Preserving Order? xSicKxBot 0 29 10-01-2020, 02:21 AM Last Post: xSicKxBot [Tut] How To Update A Key In A Dictionary In Python If The Key Doesn’t Exist? xSicKxBot 0 31 09-29-2020, 09:02 PM Last Post: xSicKxBot [Tut] List Changes After Assignment — How to Clone or Copy It? xSicKxBot 0 31 09-27-2020, 12:14 AM Last Post: xSicKxBot [Tut] How To Format A String That Contains Curly Braces In Python? xSicKxBot 0 30 09-25-2020, 07:51 PM Last Post: xSicKxBot [Tut] Python One Line Dictionary xSicKxBot 0 36 09-21-2020, 05:58 PM Last Post: xSicKxBot [Tut] Python Import Error (ModuleNotFoundError) xSicKxBot 0 32 09-20-2020, 04:15 PM Last Post: xSicKxBot

Forum Jump:

Become a Patron!
 Upcoming Events

 Latest Threads News - Review: Horace – A Heart-Warming ... Last Post: xSicKxBot Yesterday 11:38 PM » Replies: 0 » Views: 0 News - Matthew McConaughey Explains The ... Last Post: xSicKxBot Yesterday 10:20 PM » Replies: 0 » Views: 1 PlanetSide 2 External Hack / Mod Last Post: FarCry10 Yesterday 05:27 PM » Replies: 6 » Views: 6811 (Indie Deal) FREE Defense of Roman Brita... Last Post: xSicKxBot Yesterday 03:53 PM » Replies: 0 » Views: 3 AppleInsider - Lowest iPad Pro prices: s... Last Post: xSicKxBot Yesterday 03:19 PM » Replies: 0 » Views: 3 Microsoft - CVP Tom McGuinness: Top take... Last Post: xSicKxBot Yesterday 03:19 PM » Replies: 0 » Views: 3 News - Nintendo First Revealed The Switc... Last Post: xSicKxBot Yesterday 03:19 PM » Replies: 0 » Views: 3 News - Luigi Can Be Unlocked As A Secret... Last Post: xSicKxBot Yesterday 03:19 PM » Replies: 0 » Views: 3 Xbox Wire - Announcing Details for Xbox ... Last Post: xSicKxBot Yesterday 01:19 PM » Replies: 0 » Views: 4 News - Report: China-owned mobile studio... Last Post: xSicKxBot Yesterday 01:19 PM » Replies: 0 » Views: 5

 Twitter Tweets by _SickGaming