How to Find First and Last Element of Tuple in Python

In this tutorial, learn how to find first and last element of tuple in Python. The short answer is: use the index operator([]) to access the first element and the last element of the tuple using Python. You can get your single element of tuple with the methods given here.

In addition to this, you can also get elements from the first to the end of a given tuple using Python. You may also like to read how to create a tuple variable using Python

Get the First Element Of Tuple Using Python

If you want to get the first element of the tuple, you have to use the index operator([]). You have to pass zero (0) as the argument of the index operator.

The first element starts with the zero(0) index and places inside the tuple in Python. See the example below and use it to get the first element from the tuple.

Output

Bihar

The above example gives the first element as the single element in the output. The variable contains 4 elements and prints “Bihar” which is the first item. The output prints items without the parenthesis or round brackets.

Bonus: download a Free Python cheat sheet that will show you 20+ most important examples to learn in Python.

Find the Last Element Of Tuple Using Python

If you want to find the final item from the tuple, you have to pass -1 as an argument of an index operator. It finds the last items from the variable and prints them in the output. Check and use the below-given example to your last item from the tuple.

Output

13

The above example contains the last element which is 13. There are four elements in the tuple of the above example. The output gives a single item which the end item of the tuple using Python.

Access Start to End Elements of Tuple in Python

In addition to the above all methods, there is another simple method to get all items of the variable. To get all the elements, you don’t have to use the index operator. Use the tuple variable without any index operator to get all elements in the output.

Output

(‘Bihar’, 4, 29, 13)

The above example prints all the elements from the first to final items in the output. There are four elements in the tuple including the string and integer.

I hope you like this post on how to find first to final items from the tuple using Python.

DOWNLOAD Free PYTHON CHEAT SHEET

References