Python program to get first and last elements from a tuple


Tuples are an important data type in Python and are often used to store a fixed set of elements. In this article, we will be discussing how to extract the first and last elements from a tuple in Python. We will go over the syntax for accessing these elements and will provide examples of how to do so.

What is a Tuple in Python?

Tuples allow for the storage of several things in a single variable. One of python’s four built-in data types for storing data collections is the tuple.

Unchangeable and ordered collections are called tuples. Round brackets are used when writing tuples.

Example

Following is an example of creating a tuple.

firstuple = ("apple", "banana", "Cherry")
print(firstuple)

Output

('apple', 'banana', 'Cherry')

Features of a Python Tuple

Following are the points to be noted while working with tuples.

Tuple items − Duplicate values are permitted for triple items which are ordered and immutable. The first item in a triple has an index of [0], the second has an index of [1], and so on.

Example

firstuple = ("apple", "banana", "cherry", "apple", "cherry")
print(firstuple)

Output

Following is the output of the above code –

('apple', 'banana', 'cherry', 'apple', 'cherry')

Ordered − When we say that a tuple is ordered, we are referring to the fact that the items are in a specific order that won’t change.

Unchangeable − Tuples are immutable, which means that once we create a tuple, we cannot change, add, or remove any of its components.

Heterogeneous − We can create tuples with different type of values.

Example

tuple1 = ("abc", 34, True, 40, "male")
print(tuple1)

Output

('abc', 34, True, 40, 'male')

Finding the First Element of a Tuple

Using the index operator [] will allow you to retrieve the first element of the tuple. The index operator requires a single argument, which must be zero (0). The first element in a Python tuple starts with the index zero(0) and is located there. To obtain the first element from the tuple, use the example below.

Example

myTuple = ("Dehradun", 4, 29, 13)
print(myTuple[0])

Output

Dehradun

The output from the above example only contains the first element. The first item in the variable, which has four components, is printed as "Dehradun." The output does not include round brackets or parenthesis when printing items.

Finding the Last Element of a Tuple

Passing -1 as an argument to an index operator is required, if you want to find the last item in the tuple. It locates the final items in the variable and prints them in the output. Check and apply the example provided below to the last item in the tuple.

Example

myTuple = ("Dehradun", 4, 29, 13)
print(myTuple[-1])

Output

13

The final element, which is 13 in the example above, is present. The tuple from the above example has four elements. The output consists of a single item, which in Python is the last item of the tuple.

Printing all the Elements of a Tuple

There is another straightforward method to retrieve all elements of the variable in addition to the methods mentioned above. You don't need to use the index operator to get all the elements. To get all the elements in the output, use the tuple variable without using any index operators.

Example

myTuple = ("Dehradun", 4, 29, 13);
print(myTuple);

Output

('Dehradun', 4, 29, 13)

The output of the above example includes every element, from the first to the last. The string and integer are two of the tuple's four components.

Using for loop

Using a for loop, you can iterate through the items in the tuple.

Example

firstuple = ("apple", "banana", "cherry")
for x in firstuple:
   print(x)

Output

apple
banana
cherry

Looping through index

The items in the tuple can also be looped through by using their index numbers. Make a suitable iterable using the range() and len() functions.

Example

firstuple = ("apple", "banana", "cherry")
for i in range(len(firstuple)):
   print(firstuple[i])

Output

apple
banana
cherry

Using while loop

Using a while loop, you can iterate through the list items. Determine the length of the tuple using the len() function, then begin at index 0 and loop through the tuple items using the indexes. After each iteration, don't forget to raise the index by 1.

Example

firstuple = ("apple", "banana", "cherry")
i = 0
while i < len(firstuple):
   print(firstuple[i])
   i = i + 1

Output

apple
banana
cherry

Updated on: 27-Aug-2023

27K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements