This tutorial guides you on how to get the first element of a list and get the last element of a list in Python using numerous ways with examples.
Understand Slice Notation – Python
Before you learn how to get the first element of a list in Python, you should understand slice notation in Python.
As per Python documentation, you need to think that indices (both positive and negative indexes) are pointing between characters as depicted in the following diagram.
Note, the first row in the above diagram indicates positive indexes position numbers from o to 6 for the string. And second row indicates the corresponding negative indexes. When you slice the string from index i to index j, it will consists of all the characters between i and j.
For more details and examples. Please check more details on Understand Slice Notation in Python.
get the first element of a list in Python
For instance, let’s consider my_list is the given input list.
my_list = [10, 20, 30, 40, 50, 11, 22, 33, 44, 55, 66, 77]
To get the first element of the given list, you can use starting index (positive) as shown below.
By using length function and the negative index, you can find the first element of a list as shown below.
Note, if length of any given list is zero, then you will get IndexError as shown below.
If the length of the list is zero, you will get IndexError: list index out of range.
my_list --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-66-1ba9390cde38> in <module> ----> 1 my_list IndexError: list index out of range
get the last element of a list in Python
To get the last element of a list in Python, you can use starting negative index “-1” as shown below.
Similarly, to get the second to the last element of a list.
Note, you can use the negative index with the slice notation as shown below to get the last element.
And, if length of list is zero in the above case.
It wont crib IndexError, rather it will return empty list as shown below.
That’s it. You had learnt how to get the first element of a list and how to get last element of a list in Python in numerous ways.
Hope it is helpful 🙂
You’ll also like:
- Convert list of lists to flat list – flatten list in Python ?
- Concatenate or join two lists using Python
- Single digit number to double digits string conversion in Python
- String Immutability with Examples in Python
- Python Program to Check Given Number is Odd or Even
- Increase the cell width of the Jupyter Notebook in browser
- Add python3 kernel to jupyter IPython notebook ?
- Programs to Print Patterns – Pyramid, Triangle using Star
- How to change the default theme in Jupyter Notebook ?
- Change the Jupyter Notebook startup folder in Windows & Mac
- To run Jupyter Notebook on Windows from command line
- String Slicing in Python with Examples
- Difference between Python’s list methods append and extend ?
- Python equivalent for && (logical and) to use in if statement
- Split list in to chunks of size n in Python
- How to check if the given list is empty in Python ?
- Remove non-numeric characters from string in Python
- Install Python 3 on Windows 10 machine
- Convert negative to positive number in Python
- Create list of lists in Python
- Extract numbers from a string in python
- How to access index and value in for loop – Python?
- Find index of an element in a list – Python
- Prevent the list that changes unexpected after assignment – Python ?
- Get number of elements in a list – Python ?