Tuples in Python
Python tuples is a sequence used to store elements (a collection of data). Similar to arrays and lists, tuples also store elements based on their index and can be accessed based on the index position.
Understanding Python Tuples: A Core Datatype
Tuples are core data types in Python, alongside list, set, and dictionary. Tuples in Python are of type <class ‘tuple’>
. One of the key features of tuples is that they are immutable objects in Python. This means we cannot perform add (append), update (change), or delete (remove) operations on tuple objects. The size of tuple objects cannot be increased or decreased because we cannot add or remove elements from the tuple.
Exploring Python Tuple Characteristics
Python Tuples are ordered, immutable, and can contain heterogeneous elements and duplicates. They are efficient for storing fixed collections of data. Here are some characteristics of tuple in Python.
- Ordered: Tuples maintain the order of elements, meaning the elements will always appear in the same sequence they were added.
- Immutable: Once a tuple is created, its contents cannot be changed. You cannot modify, add, or remove elements.
- Allow Duplicates: Tuples can contain duplicate values, meaning the same element can appear multiple times in a tuple.
- Heterogeneous Elements: Tuples can store items of different data types, such as integers, strings, and lists.
- Supports Indexing, Slicing, and Iteration: You can access elements by index, slice tuples, and loop through them like other collections.
- Memory Efficient: Due to their immutability, tuples use less memory compared to lists, making them more memory-efficient.
- Single Element Tuple: To define a tuple with one element, you need to include a trailing comma.
- Nested Tuples: Tuples can contain other tuples, allowing for nested structures.
- Concatenation and Repetition: Tuples support concatenation (combining two tuples) and repetition (repeating elements in a tuple).
Tuple Operations in Python
Although tuple are immutable objects but we can create new tuple while performing add, update and delete operations on tuples. We have covered each operation that we can perform on tuple in Python in detail. Here is brief summary of of common tuple operations in Python.
- Accessing Elements: You can access elements using their index. Example:
shbytes_tuple[0]
- Slicing: You can extract a part of the tuple using a range of indices. Example:
shbytes_tuple
[1:3] - Concatenation: You can combine two tuples using the
+
operator. Example:tuple_1 + tuple_2
- Repetition: You can repeat a tuple using the
*
operator.Example:shbytes_tuple
* 2 - Membership Test: Check if an element exists in a tuple using
in
.Example:5 in
shbytes_tuple
- Counting Elements: Use
.count()
to count occurrences of an element.Example:shbytes_tuple
.count(2) - Finding Index: Use
.index()
to find the first index of an element.Example:shbytes_tuple
.index(3) - Length: Use
len()
to get the number of elements.Example:len(
shbytes_tuple
)
Code snippets and programs related to Tuple operations in Python, can be accessed from GitHub Repository. This GitHub repository all contains programs related to other topics in Python tutorial.
Tuple vs List
Operation | Tuple | List |
---|---|---|
Mutability | Immutable. Once created, the elements of a tuple cannot be modified, added, or removed. | Mutable. You can modify, add, or remove elements in a list after it’s created. |
Syntax | shbytes_tuple = (1, 2, 3) | shbytes_list = [1, 2, 3] |
Performance | tuples are immutable, they are generally faster than lists for iteration and access. They also use less memory. | Lists are slower than tuples when it comes to performance, especially when dealing with large datasets. |
Use Cases | Used when the data should not change. For example, storing fixed values like coordinates (x, y, z) , or as dictionary keys (because tuples are hashable). | used when the data may need to be changed, such as when managing collections that need to be modified, added to, or reordered. |
Methods and Operations | Limited methods (e.g., count() and index() ), as it is immutable | wide range of methods for manipulation like append() , extend() , insert() , remove() , pop() , etc. |
Immutability and Hashing | Because tuples are immutable, they can be used as keys in dictionaries and sets. | Lists cannot be used as dictionary keys because they are mutable and not hashable. |
Nested Structures | You can have nested tuples, but the immutability remains in effect for the tuple itself. | Lists can contain mutable objects, allowing for nested lists with varying degrees of modification. |
Related Topics
- Tuples in Python: Operations, Definition & ExamplesTuples in Python Python tuples is a sequence used to store elements (a collection of data). Similar to arrays and lists, tuples also store elements based on their index and can be accessed based on the index position. Understanding Python Tuples: A Core Datatype Tuples are core data types in Python, alongside list, set, and…
- Create Tuple in Python: Methods, Examples, and ExplanationA tuple is a sequence datatype used to store elements (a collection of data). Tuples in Python are immutable objects, meaning we cannot perform add (append), update (change), or delete (remove) operations on tuple objects. The size of tuple objects cannot be increased or decreased because we cannot add or remove elements from the tuple.…
- Access Tuple Elements by Index in Python: Positive and Negative Indexing ExplainedElements in the tuple are stored based on their index positions. Index assignment to tuple elements is similar to index assigned in Array elements. We can access tuple elements by index index positions. Let’s first understand how index positions are assigned to tuple elements. Read article, to learn more about How to Access List Elements…
- Tuple index method – PythonElements in the tuple are stored based on their index positions. Tuple in Python provides an index(arg) method to get the index position of an element in the tuple. Follow quick-start guide, for all topics related to Tuple in Python – Tuple in Python – Quickstart In previous article Access tuple elements by index –…
- Append elements in tuple – PythonTuple is an immutable datatype in Python. Elements in a tuple cannot be appended (added) directly. To append an element into a tuple, we need to follow following steps: convert tuple in list (mutable datatype), then append the element into list and then convert list back to tuple. Follow quick-start guide, for all topics related…
- Change elements in tuple – PythonTuple is an immutable datatype. Elements in a tuple cannot be changed or replaced directly. But we can convert a tuple into a list and then can perform the change or replace operation on its elements. Follow quick-start guide, for all topics related to tuple in Python – Tuple in Python – Quickstart In previous…
- Remove elements in tuple – PythonTuple is an immutable datatype in Python. Elements in a tuple cannot be removed (deleted) directly. To remove an element from a tuple, we need to follow following steps: convert tuple in list (mutable datatype), then remove the element from list and then convert list back to tuple. Follow quick-start guide, for all topics related…
- Slicing of tuple – PythonSlicing of tuple means taking out a part (slice) of the tuple. Slicing of tuple in Python can be done using either of two ways: Follow quick-start guide, for all topics related to tuple in Python – Tuple in Python – Quickstart In article Access tuple elements by index – Python, we learned about accessing…
- Concatenate tuple – PythonConcatenation of tuples means merging of elements from two or more tuples into a single tuple. Follow quick-start guide, for all topics related to tuple in Python – Tuple in Python – Quickstart As in the previous article, we learned about Slicing of tuple in Python. We can use slicing with concatenation to merge only…
- Packing & Unpacking of tuple – PythonPacking & unpacking are two processes that we can perform on tuples in Python. Follow quick-start guide, for all topics related to tuple in Python – Tuple in Python – Quickstart We have seen the packing process in our previous article Concatenation of tuple in Python, where we used packing to concatenate multiple tuple elements…
- Loop and Comprehension in tuple – PythonLooping is a process to repeat the similar task again and again. It provides sequential access to elements in the tuple. Comprehension provides shorter syntax to iterate (loop) through tuple elements. Follow quick-start guide, for all topics related to tuple in Python – Tuple in Python – Quickstart To learn more about packing and unpacking…
- Count & Sort methods in tuple – PythonCount & Sort are built-in methods with tuple in Python. Tuple in Python can store duplicate & multiple datatype elements. Follow quick-start guide, for all topics related to tuple in Python – Tuple in Python – Quickstart Count method in tuple Syntax of the count method: count_value = tuple.count(object) There are multiple scenarios in which…
- Python Tuple – Practice Program 1In previous articles, we learned about various functions and operations that we can perform on Tuples in Python. In this article we will work on practice programs related to Python Tuples. Program – Problem Statement We will be given a nested tuple whose elements will also be tuple. Each tuple element can have multiple numbers…
- Python Tuple – Practice Program 2In previous articles, we learned about various functions and operations that we can perform on Tuples in Python. In this article we will work on practice programs related to Python Tuples. Program – Problem Statement We will be given two tuples with number elements. We need to create all unique pair combinations from these two…