How to Get the First and Last Elements of a Python List?

First And Last Element

A list is a data structure defined using square brackets that contains elements within them. Each element is separated with the help of a comma. A list is an ordered collection of elements, i.e. it maintains the same order in which elements are added. We can access its elements using different methods.

This tutorial will teach you to get the first and last elements of a list using different methods so you can choose the most suitable one according to your requirement.

Get the first and last element of a list in Python using the Index value

Indexing is used to directly access the elements of choice within a list. The default index starts from 0 and goes on till n-1. Here n denotes the total number of elements in the respective data structure.

Accessing the first element of a list

You can access the first element from the list by using the name of the list along with index 0.

Syntax:

Use the below syntax to print the first element of a list.

print(listName[0])

where the listName refers to a list for which we want to get the first element.

Accessing the last element of a list

You can access the last element by using Negative Index. Using -1 as the index gives us the last element from the list.

Syntax:

Use the below syntax to print the last element of a list.

print(listName[-1])

where the listName refers to a list for which we want to get the last element.

Example:

a = [6, 5, 9, 7]

print("first element is  ",a[0])
print("last element is ",a[-1])

Output:

First And Last Element

Get the first and last element of a list in Python using slicing

Slicing is a powerful feature that allows you to create a new list from an existing list by specifying the starting and ending indices. The new list is a portion of the original list, and it can be used for various purposes, such as processing specific data, managing memory usage, and more.

We can use slicing to access the first and the last items in the list.

Syntax:

Use the below syntax to get the first and the last element of a list.

result = listName[::len(listName)-1]

where the listName refers to a list for which we want to get the first and last element.

Example:

a = [6, 5, 9, 7]

ans = a[::len(a)-1]
print ("The first and last elements of the list are : " + str(ans))

Output:

The output of slicing method

Get the first and last element of each tuple in a list in Python

This case is a little different from the examples above. Here we have tuples as elements of the list.

A tuple is an immutable sequence, i.e. we cannot add or remove its elements, it contains elements inside parentheses separated by commas

A list of tuples looks like this:

[("a", "b", "c"), ("c", "d", "e"), ("f","g","h")]

We have to get the first element of each tuple. Thanks to Python, we can do this using just one line of code using list comprehension.

first_tuple_elements = [a_tuple[0] for a_tuple in tuple_list]

This will create a list of all the first elements of the tuple.

To get the last elements of all tuples replace 0 with -1.

last_tuple_elements = [a_tuple[-1] for a_tuple in tuple_list]

This will create a list with all the last elements of the tuples.

Example:

tuple_list = [("a", "b", "c"),("c", "d", "e"), ("f","g","h")]
first_tuple_elements = [a_tuple[0] for a_tuple in tuple_list]
print(first_tuple_elements)

Output:

Tuple Output

Conclusion

In this tutorial, we have learned to get the first and last element of a list in Python using index value, slicing method and list comprehension. If you never want to slip up these concepts, do not forget to try them. Hope you enjoyed reading this tutorial.

Reference

https://stackoverflow.com/questions/930397/how-do-i-get-the-last-element-of-a-list