Difference between List VS Set VS Tuple in Python (original) (raw)
Last Updated : 17 Feb, 2025
In Python, Lists, Sets and Tuples store collections but differ in behavior. Lists are ordered, mutable and allow duplicates, suitable for dynamic data. Sets are unordered, mutable and unique, while Tuples are ordered, immutable and allow duplicates, ideal for fixed data.
List in Python
A List is a collection of ordered, mutable elements that can hold a variety of data types. Lists are one of the most commonly used data structures in Python due to their flexibility and ease of use.
**Key Characteristics:
- **Mutable: Elements can be modified after creation.
- **Ordered: Maintains the order of elements.
- **Allows Duplicates: Can have multiple occurrences of the same value.
- **Heterogeneous: Can store different data types. Python `
Creating a List
a = [1, 2, 3, 'Python', 3]
Accessing elements by indexing
print(a[0]) print(a[-1])
Modifying an element
a[1] = 'Updated' print(a)
Appending an element
a.append(4)
Removing an element
a.remove(3)
List slicing
print(a[1:3])
`
Output
1 3 [1, 'Updated', 3, 'Python', 3] ['Updated', 'Python']
Set in Python
A Set is an unordered collection of unique elements. Sets are primarily used when membership tests and eliminating duplicate values are needed.
**Key Characteristics:
- **Mutable: Elements can be added or removed.
- **Unordered: Does not maintain the order of elements.
- **Unique Elements: Duplicate values are automatically removed.
- **Heterogeneous: Can store different data types. Python `
Creating a Set
s = {1, 2, 3, 'Python', 3} print(s)
Adding elements
s.add(4)
Removing elements
s.remove(3) # KeyError if the element is not present
Accessing elements (No indexing because it is unordered)
for element in s:
print(element)
`
Output
{'Python', 1, 2, 3}
Tuple in Python
A Tuple is an ordered, immutable collection of elements. Tuples are often used when data should not be modified after creation.
**Key Characteristics:
- **Immutable: Once created, elements cannot be modified.
- **Ordered: Maintains the order of elements.
- **Allows Duplicates: Can contain duplicate values.
- **Heterogeneous: Can store different data types. Python `
Creating a Tuple
tup = (1, 2, 3, 'Python', 3)
Accessing elements by indexing
print(tup[0]) print(tup[-1])
Tuple slicing
print(tup[1:4])
Attempting to modify a tuple (Raises TypeError)
tup[1] = 'Updated' # Uncommenting this will raise an error
`
Output
1 3 (2, 3, 'Python')
Key Differences Between Lists, Set and Tuple
Feature | List | Set | Tuple |
---|---|---|---|
Mutability | Mutable | Mutable (elements must be immutable) | Immutable |
Ordering | Ordered | Unordered | Ordered |
Duplicates | Allows duplicates | No duplicates allowed | Allows duplicates |
Indexing | Supports indexing and slicing | Not supported | Supports indexing and slicing |
Performance | Slower for membership tests | Faster membership tests | Faster than lists |
Use Case | When frequent modifications are required | When uniqueness is needed | When immutability is required |
Similar Reads
- Python - Adding Tuple to List and Vice - Versa Adding a tuple to a list in Python can involve appending the entire tuple as a single element or merging its elements into the list. Conversely, adding a list to a tuple requires converting the list to a tuple and concatenating it with the existing tuple, as tuples are immutable. For example, adding 4 min read
- Convert List to Tuple in Python The task of converting a list to a tuple in Python involves transforming a mutable data structure list into an immutable one tuple. Using tuple()The most straightforward and efficient method to convert a list into a tuple is by using the built-in tuple(). This method directly takes any iterable like 2 min read
- Python - Test if any set element exists in List Given a set and list, the task is to write a python program to check if any set element exists in the list. Examples: Input : test_dict1 = test_set = {6, 4, 2, 7, 9, 1}, test_list = [6, 8, 10] Output : True Explanation : 6 occurs in list from set. Input : test_dict1 = test_set = {16, 4, 2, 7, 9, 1}, 4 min read
- Differences and Applications of List, Tuple, Set and Dictionary in Python Python provides us with several in-built data structures such as lists, tuples, sets, and dictionaries that store and organize the data efficiently. In this article, we will learn the difference between them and their applications in Python. Difference between List, Tuple, Set, and DictionaryThe fol 11 min read
- Time Complexity for Adding Element in Python Set vs List Adding elements differs due to their distinct underlying structures and requirements for uniqueness or order. Let's explore and compare the time complexity by adding elements to lists and sets. In this article, we will explore the differences in time complexities between a Set and a List if we add a 2 min read
- Creating Sets of Tuples in Python Tuples are an essential data structure in Python, providing a way to store ordered and immutable sequences of elements. When combined with sets, which are unordered collections of unique elements, you can create powerful and efficient data structures for various applications. In this article, we wil 3 min read
- Convert Dictionary to List of Tuples - Python Converting a dictionary into a list of tuples involves transforming each key-value pair into a tuple, where the key is the first element and the corresponding value is the second. For example, given a dictionary d = {'a': 1, 'b': 2, 'c': 3}, the expected output after conversion is [('a', 1), ('b', 2 3 min read
- Generating a "Set Of Tuples" from A "List of Tuples" - Python We are given a list of tuples and we need to extract only the unique tuples while removing any duplicates. This is useful in scenarios where you want to work with distinct elements from the list. For example:We are given this a list of tuples as [(1, 2), (3, 4), (1, 2), (5, 6)] then the output will 3 min read
- Difference Between Enumerate and Iterate in Python In Python, iterating through elements in a sequence is a common task. Two commonly used methods for this purpose are enumerate and iteration using a loop. While both methods allow us to traverse through a sequence, they differ in their implementation and use cases. Difference Between Enumerate And I 3 min read
- Python - Elements frequency in Tuple Given a Tuple, find the frequency of each element. Input : test_tup = (4, 5, 4, 5, 6, 6, 5) Output : {4: 2, 5: 3, 6: 2} Explanation : Frequency of 4 is 2 and so on.. Input : test_tup = (4, 5, 4, 5, 6, 6, 6) Output : {4: 2, 5: 2, 6: 3} Explanation : Frequency of 4 is 2 and so on.. Method #1 Using def 7 min read