Python Set vs Tuple

Python provides several built-in data structures to store collections of elements, including lists, tuples, and sets. While lists are commonly used, tuples and sets have specific use cases. Tuples are ordered collections of immutable elements, while sets are unordered collections of unique elements.

I will explain the differences between Python sets and tuples in this tutorial with some practical examples.

What are Tuples in Python?

A tuple is a collection of Python objects, much like a list. The sequence of values stored in a tuple can be of any type, and integers index them. Tuples are immutable, meaning that once created, their elements cannot be changed. Tuples are defined using parentheses () or by simply separating elements with commas.

Example of creating a tuple:

my_tuple = (1, 2, 3, 'apple', 'banana')

Tuples support indexing and slicing, just like lists:

my_tuple = (1, 2, 3, 'apple', 'banana')
print(my_tuple[0])  # Output: 1
print(my_tuple[1:4])  # Output: (2, 3, 'apple')

Here is the exact output in the screenshot below:

Python Set vs Tuple

Check out How to Use Python Type Hint Tuples for More Robust Code?

What are Sets in Python?

Sets are unordered collections of unique elements. They are defined using curly braces {} or the set() constructor. The major difference between sets and lists or tuples is that sets cannot have multiple occurrences of the same element and store unordered values.

Example of creating a set:

my_set = {1, 2, 3, 'apple', 'banana'}

Since sets are unordered, you cannot access elements using indexing or slicing. However, sets provide powerful methods for mathematical set operations like union, intersection, and difference.

set1 = {1, 2, 3}
set2 = {3, 4, 5}

print(set1.union(set2))  # Output: {1, 2, 3, 4, 5}
print(set1.intersection(set2))  # Output: {3}
print(set1.difference(set2))  # Output: {1, 2}

Here is the exact output in the screenshot below:

Differences between Sets and Tuples in Python

Read How to Print a Tuple in Python?

Key Differences between Sets and Tuples

  1. Mutability:
    • Tuples are immutable, meaning their elements cannot be changed once created.
    • Sets are mutable, allowing addition or removal of elements.
  2. Ordering:
    • Tuples maintain the order of elements as defined.
    • Sets are unordered and do not preserve the order of elements.
  3. Duplicates:
    • Tuples can contain duplicate elements.
    • Sets cannot contain duplicate elements; they only store unique values.
  4. Indexing and Slicing:
    • Tuples supports indexing and slicing to access elements according to their position.
    • Sets do not support indexing or slicing since they are unordered.
  5. Mathematical Set Operations:
    • Sets provide built-in methods for mathematical set operations like union, intersection, and difference.
    • Tuples do not have built-in support for set operations.

Read How to Check if a Tuple is Empty in Python?

When to Use Tuples vs Sets

The purpose of a tuple is to represent an entity without using a class. The purpose of a set is to hold multiple unique items. Let me explain when to use tuple vs sets in Python.

Use tuples when:

  • You have a fixed collection of elements that shouldn’t change.
  • The order of elements is important and needs to be preserved.
  • You want to use the collection as a key in a dictionary.

Use sets when:

  • You need to store a collection of unique elements.
  • The order of elements is not important.
  • You want to perform mathematical set operations like union, intersection, or difference.

Check out How to Unpack a Tuple in Python?

Python Set vs Tuple: Summary

Here is a summary of the differences between Python set vs tuple.

FeatureTupleSet
MutabilityImmutableMutable
OrderingOrderedUnordered
DuplicatesAllowedNot Allowed
Indexing/SlicingSupportedNot Supported
Set OperationsNot SupportedSupported

Conclusion

Tuples are immutable and ordered, making them suitable for representing fixed collections where element order matters. Sets, on the other hand, are mutable and unordered, ideal for storing unique elements and performing set operations. In this tutorial, I explained when to use tuple vs set in Python with some real examples.

You may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.