Access Tuples In Python

We are going to learn Python tuples access with examples in this article, in hopes that it will be useful for education.

Python Access Tuples Items

When working with Python access tuples, you can access tuple items by their index number, enclosed in square brackets []:

In the tuple, print the fourth item:

Example: 

country_tuple = ("United States of America", "United Kingdom", "Finland", "Brazil", "Germany", "Spain", "Maldives", "Croatia", "Denmark", "Italy") print(country_tuple[3])

Reminder: It is important to note that the first item has index 0.



Range of Indexes

The Python access tuples allows you to set a starting point and an end point for a range of indexes.

The returned tuple contains all items in the specified range if the range is defined.

The fourth, fifth, and sixth items should be returned as follows:

Example: 

country_tuple = ("United States of America", "United Kingdom", "Finland", "Brazil", "Germany", "Spain", "Maldives", "Croatia", "Denmark", "Italy") print(country_tuple[3:6])

Reminder: It should be noted that the search will start at index 3 (included) and end at index 6 (not included).

You must remember that the index of the first item is 0.

In the absence of a start value, the range will start at the first item in the list.

In this example, items are returned from the beginning to, but not including, “Spain”:

Example: 

country_tuple = ("United States of America", "United Kingdom", "Finland", "Brazil", "Germany", "Spain", "Maldives", "Croatia", "Denmark", "Italy") print(country_tuple[:5]) #The items from index 0 to index 5 will be returned by this method. #Remember that index 0 represents the first item, and index 5 represents the sixth item. #It is important to remember that the item in index 5 is not included.

 

If the range doesn’t contain a value at the end, it will go on to the end of the list.

In this example, we return the items from “Brazil” to the end:

Example: 

country_tuple = ("United States of America", "United Kingdom", "Finland", "Brazil", "Germany", "Spain", "Maldives", "Croatia", "Denmark", "Italy") print(country_tuple[3:]) #This will return the items from index 3 to the end of the list. #It is important to remember that index 0 is the first item, and index 1 is the second item.

Negative Indexing

Python access tuples negative indexing means starting from the end.

  • The last item is numbered -1,
  • The -2 indicates the second last item, etc.

Take the last item from the tuple and print it:

Example: 

country_tuple = ("United States of America", "United Kingdom", "Finland", "Brazil", "Germany", "Spain", "Maldives", "Croatia", "Denmark", "Italy") print(country_tuple[-1])

Check if Item Exists

If we are talking about Python access tuples, we can use the in keyword to check if an item exists.

Verify that “Germany” appears in the list:

Example: 

country_tuple = ("United States of America", "United Kingdom", "Finland", "Brazil", "Germany", "Spain", "Maldives", "Croatia", "Denmark", "Italy") if "Germany" in country_tuple: print("Yes, 'Germany' is in the Country tuple")

Range of Negative Indexes

In Python access tuples, specify negative indices if you would like to start searching from the end.

Using this example, we return all items from “Brazil” (-7) to, but exclude “Italy” (-1):

Example: 

country_tuple = ("United States of America", "United Kingdom", "Finland", "Brazil", "Germany", "Spain", "Maldives", "Croatia", "Denmark", "Italy") print(country_tuple[-7:-1]) #The term "negative indexing" refers to starting at the end of the list. #The following example returns items from index -7 (included) to index -1 (excluded). #Keep in mind that the last item has an index of -1.

 

Share your thoughts and help us improve! Leave your reaction below to appreciate our efforts or provide suggestions for this site.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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