In this Python tutorial, we will learn how to access items of a tuple in Python. To understand various approaches, we’ll use some built-in functions to access items of a tuple in Python with some demonstrative examples.
As a Developer, while making the Python Project, I got the requirement to access items of a tuple in Python. So, I came across multiple methods that we can use to access items of a tuple in Python. In this tutorial, we will try to cover all of them.
Methods to access items of a tuple in Python
There are several different methods present in Python to access tuple:
- Using index
- for loop
- Using a negative index
- Python slicing
- Using if else condition
Let’s see them one by one using some illustrative examples.
Method 1: How to access items of a tuple in Python using an index
- In this section, we will discuss how we can access items of a tuple in Python using index numbers.
- A Python Tuple is both an iterable and an ordered group of objects. As a result, we can traverse over things using the for or while loops, or we can read the values of objects using their indexes.
- Elements are enclosed in parenthesis () and separated from one another by commas to form a tuple in Python. The declaration of a tuple-type variable is as follows.
Example: Let’s take an example and check how we can access items of a tuple in Python using an index number.
Source Code:
Country_name = ('U.S.A', 'Germany', 'Australia', 'China', 'NewZealand')
new_output = Country_name[1]
print("Access values in tuple: ",new_output)
Output: In the following given code first, we created a tuple in Python and by using the index number we can easily access the elements from it. For this, we are just providing the index number inside the square brackets beside the name of the Python tuple.
Access values in tuple: Germany
Here is the execution of the following given code.
This is how to access items of a tuple in Python using an index.
Method 2: How to access items of a tuple in Python using for loop
- Now let us discuss how we can access items of a tuple in Python using a for loop.
- Tuples are iterable objects, thus we can get their values by iterating over their items using a for loop.
Example: Let’s consider a situation where we have to get all the elements present inside a tuple.
new_numbers = (34, 12, 98)
for result in new_numbers:
print("Access value in tuple :",result)
Output: Here, we are using a for loop to iterate over the Python tuple and print every single element inside the tuple using the print() statement.
Access value in tuple : 34
Access value in tuple : 12
Access value in tuple : 98
Here is the Screenshot of the following given Python code:
As you can see in the Screenshot we discussed how to access items of a tuple in Python using for loop.
Method 3: How to access items of a tuple in Python using a negative index
- In this section, we will discuss how we can access a tuple in Python using a negative index number.
- Here we will use negative indexing where negative indexing begins at the end of the tuple with -1 and decreases from right to left.
Example: Let’s take an example and check how to access tuple in Python using a negative index.
Source Code:
Country_name = ('U.S.A', 'China', 'Australia', 'Germany', 'United Kingdom')
new_result = Country_name[-1]
print("First item of tuple :", new_result)
new_output = Country_name[-2]
print("Second item of tuple :",new_output)
result = Country_name[-3]
print("Third item of tuple :", result)
Output: In the following given code first, we declared a tuple in Python. Next, we will iterate items through a tuple by using a negative index. First, we iterate our first item by [-1]. Then, [-2], and at last [-3] to get the last three elements of the Python tuple.
First item of tuple : United Kingdom
Second item of tuple : Germany
Third item of tuple : Australia
Here is the screenshot of the following given code:
In this example, we understood how to fetch items of a tuple in Python using negative indexing.
Method 4: How to access items of a tuple in Python using slicing
- Now let us understand how to fetch items of a tuple in Python using slicing.
- Python slicing is about obtaining a sub-string from the given string by slicing it. For getting the part of the string we will specify the start index and the end index, separated by the colon.
- The slicing operator colon allows us to access a variety of tuple items: and in this example, we will access the second and fourth index values.
Example: Here we will take an example and check how to access the items of a tuple in Python using slicing.
Source Code:
# Creating a tuple
Cities_of_USA = ('New York', 'Los Angeles',' Chicago','Atmore')
# Access elements 2nd to 4th.
print(Cities_of_USA[1:4])
Output: Here, we are using slicing in Python, where are starting with the 1 index number and ending (excluding) with the 4 index number.
('Los Angeles', ' Chicago', 'Atmore')
You can refer to the below screenshot:
This is how to access the items of a tuple in Python using slicing.
Method 5: How to access items of a tuple in Python using if else
- In this section, we will discuss how to access the items of a tuple in Python using the if else condition.
- If conditional statement will run if the provided item is present inside the tuple in Python and if not then the else statement will run.
Example: Let’s take an example and check how to access the items of a tuple in Python using the if else condition.
Source Code:
new_tuple=('John', 'Micheal','George')
if 'Micheal' in new_tuple:
print ("Accessed item is present in tuple :", 'Micheal' )
else:
print("Accessed item is not present in tuple")
Output: In the following given code, we created a tuple in Python, then with the help of the if-else condition, we will check if the item is present in the tuple, and then print it.
Accessed item is present in tuple : Micheal
Here is the execution of the following given code:
As you can see in the Screenshot we discussed how to access the items of a tuple in Python using the if-else conditional statement.
Conclusion
In this article, we have discussed how to access items of a tuple in Python. To understand various approaches, we used some built-in functions to access the items of a tuple in Python with examples like index number, for loop, negative index, slicing, and if else conditional statement.
Now, the choice depends on the programmer which method to select for their program and problems.
You may also like to read the following Python tutorials.
- Concatenate tuples in Python
- Convert List of Tuples to String in Python
- Python program to sort list of tuples
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.