How to Find First and Last Element of Tuple in Python

Accessing the first element and the last element of the tuple requires index operator([]). You can find the first element and last element of tuple using Python. Get your single element of tuple with the methods given here.

In addition to this, you can also get elements from first to the end of given tuple using Python. You may also like to read how to create 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 items. The output prints items without the parenthesis or round brackets.

Find 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 print 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 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.

Hope, you like this post of how to find first to final items from the tuple using Python. If you have any query regarding the tutorial, please comment below.

References

Also tell me, which method you are using to get the start and final elements using Python.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.