Python List - Get First Element


Python List - Get first element

To get the first element of a list in Python, you can use the index 0 with the list variable.

The index of the elements in the list start from 0. Therefore, element at index=0 is the first element in the list.

In this tutorial, you will learn how to get the first element in a given list using index, with examples.

For example, if my_list is a given list of elements, then the syntax to the expression to get the first element in the list my_list is

my_list[0]

Examples

The following are some of the examples, where we take a list of elements, and get the first element from the list using index.

1. Get the first element in the list of integers in Python

In this example, we take a list of integer values, and get the first element using index 0, and print it to standard output.

Python Program

my_list = [10, 20, 30, 40]
first_element = my_list[0]
print(f"First element : {first_element}")

Output

First element : 10

Explanation

10, 20, 30, 40  ← list elements
 0   1   2   3  ← index values
 ↑
element at index=0 is the first element

2. Get the first element in the list of strings in Python

In this example, we take a list of string values, and get the first element using index 0.

Python Program

my_list = ["apple", "banana", "cherry"]
first_element = my_list[0]
print(f"First element : {first_element}")

Output

First element : apple

Explanation

"apple", "banana", "cherry"  ← list elements
 0        1         2        ← index values
 ↑
element at index=0 is the first element

Summary

In this Python Lists tutorial, we have seen how to get the first element of the given list using index.