What is a Python Tuple?
A Python Tuple is an ordered, immutable collection of items. The “items” within the Tuple may be of different of data types. The items may also be other collection data types like dictionaries, lists’s, arrays or even another tuple.
Make sure you really want to be working with tuples! There are other data types out there like arrays, lists and dictionaries which may be more suitable for some tasks. First scope out your problem and then decided which data type fits the best.
Python Tuple Syntax
newtuple = ("Python","Java","C++","Ruby")
The tuple must be enclosed within round brackets, and each item separated by commas.
The double quotes are not part of the syntax of the tuple. They are only necessary if the item is a string, as strings are always enclosed in double quotes.
Tuple Indexing
You can access the contents of a tuple by using it’s index. A tuple is an ordered collection, so the values can be called in the order in which they were placed inside.
Don’t forget that indexing starts from zero in python. The code below will output “Java”.
newtuple = ("Python","Java","C++","Ruby")
print(newtuple[1])
Using negative integers will reverse the order of the tuple (Negative indexing starts from -1)
newtuple = ("Python","Java","PHP","C++","Ruby")
print(newtuple[-1])
#Output
Ruby
Selecting a range of items
If you wish to pick up more than just one item in a python tuple, you can create “slices”. You can specify a range, by giving the starting and ending number. Everything in between will be placed into the slice.
Note that the ending number will not be included in this slice.
newtuple = ("Python","Java", "PHP", "C++","Ruby")
print(newtuple[1:4])
# Output
Java
PHP
C++
You don’t necessarily how to define the starting point, as the default will be 0, or the start of the tuple. The semi-colon and ending point must be present though.
newtuple = ("Python","Java", "PHP", "C++","Ruby)
print(newtuple[:4])
#Output
Python
Java
PHP
C++
It also works the other way around, where you define the start point, and leave out the end point, while keeping the semi-colon.
newtuple = ("Python","Java", "PHP", "C++","Ruby")
print(newtuple[2:])
# Output
PHP
C++
Ruby
Changing Values in a Tuple
As a python tuple is a immutable data type, which means that once it has been created, you cannot change any of it’s value. This prevents you from adding, updating or deleting anything from the tuple. There are some ways to work around this, with varying degrees of success.
The first is converting a tuple to list, changing whatever value you wish, then changing it back.
tuple1 = (1,2,3,4,5)
list1 = list(tuple1)
list1.append(6)
tuple1 = tuple(list1)
print(tuple1)
The second, would be concatenating another tuple onto the first tuple. Example shown further on into the page.
But generally, if you wish for a collection data type where you can change values, you shouldn’t be using Tuples in the first place.
Deleting a Tuple
You may not be able to delete individual items, but you can delete the whole tuple in one go using the del
keyword.
newtuple = ("Python","Java","C++")
del newtuple
Other Useful Operations
To check for a specific item in a Tuple
tuple = (1,2,3,4,5)
if 2 in tuple:
print("Found")
Finding the length of a Tuple
tuple = (1,2,3,4,5)
length = len(tuple)
Iterating through a Tuple
The following code will iterate over every element in the tuple and print them out.
tuple = (1,2,3,4,5)
for x in tuple:
print(x)
Concatenating two Tuples
The following code all both tuples together and produce a tuple containing the elements of both tuple1 and tuple2.
tuple1 = (2,3,4,5)
tuple2 = ("a","b","c")
tuple3 = tuple1 + tuple2
Why use Python Tuples?
Why would anyone use a Python Tuple over a list, when the list can be used for a greater number of things? The answer is contained below.
- Tuples are faster than lists. Tuples use less memory than Lists as they are immutable. Lists use two blocks of memory as comparison to one used by Tuples.
- Tuples can be used keys in dictionaries. Lists cannot be used for the same as they are mutable. Dictionaries only accept immutable values as keys.
Unless you intend to be adding or removing items in a list during your programming, using tuples is the way to go.
Extra Example(s)
Finding the index of an item. The index()
function comes in handy here.
tuple = ("Python", "Java","C++")
print(tuple.index("Java"))
This marks the end of the Python Tuple page. Feel free to make any suggestions or contributions. Anything to help improve CodersLegacy is more than welcome.