yellow leaves near green leaves
Photo by Ryan Song on Unsplash

Lists represent one of the most used data structures in any Python script. One of the reasons why I like programming in Python is that I get to reach a result quite quickly.

One such thing is the opportunity to easily and quite simply access their elements quite flexibly.

With flexibility, I mean, having the opportunity to choose only the elements that we want.

For example, how can we access the last element in a list in Python?

We can get it using the index of -1 and we immediately get the last element:

There is also an easy way to get only the elements that are in the middle of a list, meaning all the elements excluding the first and the last one.

We can do that by removing these last two elements, such as:

We can also do that in a single line by using the underline symbol.

When we want to ignore an element in Python, we can use the underline symbol.



Now you have both these methods that you can use and you can choose the one that you prefer more.

I hope you find this useful.