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.
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 the items of a tuple in python. And in this tutorial, we will try to cover all of them.
Here we will see:
- How to access items of a tuple in Python using an index
- How to access items of a tuple in Python using for loop
- How to access items of a tuple in Python using a negative index
- How to access items of a tuple in Python using slicing
- How to access items of a tuple in Python using tuple
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.
- 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. 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.
Source Code:
Country_name = ('U.S.A', 'Germany', 'Australia', 'China', 'NewZealand')
# Using by index
new_output = Country_name[1]
# Display the Content
print("Access values in tuple: ",new_output)
In the following given code first, we created a tuple named ‘Country_name’ and by using the index we can easily access the elements.
Here is the execution of the following given code.
This is how to access items of a tuple in Python using an index.
Read: Python concatenate tuples
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 for loop.
- Tuples are iterable objects, thus we can get their values by iterating over their items using a for loop.
Example:
new_numbers = (34, 12, 98)
for result in new_numbers:
print("Access value in tuple :",result)
Here is the Screenshot of the following given code.
As you can see in the Screenshot we discussed how to access items of a tuple in Python using for loop.
Read: Python sort list of tuples
How to access items of a tuple in Python using a negative index
- In this section, we will discuss how we can fetch items of a tuple in Python using a negative index.
- 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 the items of a 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) #U.S.A
new_output = Country_name[-2]
print("Second item of tuple :",new_output) #Germany
result = Country_name[-3]
print("Third item of tuple :", result) #Australia
In the following given code first, we declared a tuple named Country_name. Next, we will iterate items through a tuple by using a negative index. In this example first, we iterate our first item by [-1].
Here is the screenshot of the following given code.
In this example, we understood how to fetch items of a tuple in Python using a negative index.
Read: How to create a list of tuples in Python
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 index
print(Cities_of_USA[1:4])
You can refer to the below screenshot.
This is how to access the items of a tuple in Python using slicing.
Read: Python program to create empty set
How to access items of a tuple in Python using tuple
- In this section, we will discuss how to access the items of a tuple in Python using a tuple.
- For iterating each element we are using for loop in python and also accessing items of a tuple.
Example:
Let’s take an example and check how to access the items of a tuple in Python using a tuple.
Source Code:
new_tuple=('John', 'Micheal','George')
for i in new_tuple:
print ("Access elements of tuple :", i)
In the following given code, we created a tuple named ‘new_tuple’ and then iterate a values using for loop.
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 a tuple.
You may also like to read the following Python tutorials.
- Union of sets Python + Examples
- How to Add Elements in a Set in Python
- Python program for intersection of sets
In this article, we have discussed how to access a tuple’s items in Python. To understand various approaches, we used some built-in functions to access the items of a tuple in Python.
- How to access items of a tuple in Python using an index
- How to access items of a tuple in Python using for loop
- How to access items of a tuple in Python using a negative index
- How to access items of a tuple in Python using slicing
- How to access items of a tuple in Python using tuple
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.