Future-Proof Your Career, Master Data Skills + AI

Blog

Future-Proof Your Career, Master Data Skills + AI

Blog

Python Set vs List – The Real Difference

by | 6:04 pm EDT | April 14, 2023 | Python

In the world of Python programming, you will often encounter various data structures that serve different purposes. Among these structures, sets, and lists are commonly used for storing and manipulating collections of data.

While they may appear similar, there are notable differences in their properties and use cases.

Python Set vs List

Understanding Python Sets and Lists

Ok, so let’s explain things first.

What are Python Sets?

A Python set is a built-in data structure that represents an unordered collection of distinct elements, called members.

This powerful tool is particularly useful in data science applications and mathematical operations.

Python sets have the following properties:

  1. They are unordered, which means that elements in a set are stored independently of their index. This unordered collection of unique values supports efficient membership tests and set comprehensions.

  2. They do not allow duplicate values. This makes them useful when you need to work with unique values, remove duplicates from a list, or perform set operations like unions, intersections, and symmetric differences.

There are two ways to create a set in Python:

  1. By using curly braces ({}), also known as curly brackets.

  2. By using the built-in set() function, which takes a single argument, an iterable containing the elements you want to include in the set.

A generic syntax for creating a Python set using curly braces and the built-in set function is given below:

my_set = {1, 2, 3}
another_set = set([4, 5, 6])

What Are Python Lists

A Python list is a built-in data structure similar to dynamic arrays in other programming languages.

They are used to store multiple items in a single variable, making them a versatile option for handling various data types, such as strings, numbers, and booleans.

Python Lists have the following properties:

  1. They are ordered, which means that a particular element has a unique position in a list and can be accessed through its index. This ordered collection supports random access, allowing you to perform operations such as slicing, concatenation, and list comprehension.

  2. They are mutable, and their elements can be changed after creating a list, offering flexibility when working with data structures.

  3. Python lists allow for duplicate values and can store a mix of data types, including strings, numbers, and booleans.

There are two ways to create a list in Python:

  1. By using square brackets, which denote the boundaries of the list.

  2. By using the built-in list() function, which can take a single argument or an iterable containing the elements you want to include in the list.

The following Python code demonstrates creating a Python list using square brackets and the built-in list() function:

list1 = [1, 2, 3] 
list2 = list([4, 5, 6])

3 Key Differences Between Sets and Lists

They are multiple differences between a Python set and a list. Some of the important ones are listed below:

1. Order and Indexing

Order and Indexing in Python List: A Python list can support indexing, meaning you can access elements in a list using their position in the list. This provides flexibility when manipulating data with a known order.

The following Python code demonstrates the order and indexing of lists:

# Creating a Python list
my_list = [3, 5, 2, 8, 1]

# Accessing elements using their index
first_element = my_list[0]  # This will be 3
third_element = my_list[2]  # This will be 2

# Modifying elements using their index
my_list[1] = 7  # The list becomes [3, 7, 2, 8, 1]

# Iterating over a list maintaining the order
for item in my_list:
    print(item)
Order and Indexing in Python Lists

Python Sets: A Python set is an unordered collection with no indexing, which means you cannot access elements using their position. This is useful when the order of elements does not matter.

The following Python code demonstrates order and indexing sets:

# Creating a Python set
my_set = {3, 5, 2, 8, 1}

# Sets are unordered, so you cannot access elements using their position
# This would raise an error: first_element = my_set[0]

# Modifying a set by adding or removing elements
my_set.add(6)       # The set becomes {1, 2, 3, 5, 6, 8}
my_set.discard(5)   # The set becomes {1, 2, 3, 6, 8}

# Iterating over a set (order is not guaranteed)
for item in my_set:
    print(item)
Order and Indexing in Python Sets

2. Mutability

Python List: A Python list is mutable, allowing you to modify its elements. They can hold any type of object, including nested lists, thereby offering more flexibility in terms of the content they can store.

The following code demonstrates mutability in Python lists:

# Creating a Python list
my_list = [3, 5, 2, 8, 1]

# Modifying the list by appending elements
my_list.append(4)  # The list becomes [3, 5, 2, 8, 1, 4]

# Modifying the list by removing elements
my_list.remove(2)  # The list becomes [3, 5, 8, 1, 4]

# Lists can hold any type of object, including nested lists
nested_list = [1, 2, [3, 4], 5]
Mutability of Python Lists

Python Set: Just like a list in Python, a Python set is also mutable and can be modified. However, sets in Python can only hold hashable (immutable) objects, meaning that you cannot have a set of sets or a set containing mutable objects like lists.

The following code demonstrates the mutability of Python sets:

# Creating a Python set
my_set = {3, 5, 2, 8, 1}

# Modifying the set by adding elements
my_set.add(6)       # The set becomes {1, 2, 3, 5, 6, 8}

# Modifying the set by removing elements
my_set.discard(5)   # The set becomes {1, 2, 3, 6, 8}

# Sets can only hold hashable (immutable) objects
valid_set = {1, 2, 3, 4, (5, 6)}

# The following would raise an error because lists are mutable and cannot be stored in sets
# invalid_set = {1, 2, [3, 4]}
Mutability of Python Sets

Uniqueness of Elements

Python Sets: A key feature of sets is that they only store unique elements. The addition of duplicate values to a list is ignored. This makes a set object ideal for different set operations, such as removing duplicates or checking the presence of unique elements.

# Creating a Python set with duplicate elements
my_set = {3, 5, 2, 8, 1, 3, 2, 5}
# The duplicate elements are automatically removed: {1, 2, 3, 5, 8}

# Checking for the presence of a unique element
if 5 in my_set:
    print("5 is in the set")
# Output: 5 is in the set

# Removing duplicates from a list using a set
my_list = [3, 5, 2, 8, 1, 3, 2, 5]
unique_list = list(set(my_list))
# The unique_list becomes [1, 2, 3, 5, 8]
  • Duplicates removal in Python Sets

    Python Lists: Lists allow duplicate values and maintain their order, which can be essential in use cases where duplicates and the order of elements play a significant role.

    # Creating a Python list with duplicate elements
    my_list = [3, 5, 2, 8, 1, 3, 2, 5]
    # The list contains duplicate values: [3, 5, 2, 8, 1, 3, 2, 5]
    
    # Checking for the presence of an element in a list
    if 5 in my_list:
        print("5 is in the list")
    # Output: 5 is in the list
    
    # Counting the occurrences of a value in a list
    count_of_5 = my_list.count(5)
    print("5 appears", count_of_5, "times")
    # Output: 5 appears 2 times
    Duplicate values operations in Python Lists

3. Supported Operations

There are different operations one can perform on sets and lists, each optimized for specific tasks:

Python Lists: Due to their ordered and index-based nature, lists support operations like slicing, concatenation, repetition, and list comprehension. They also provide built-in methods, such as append(), pop(), and sort(), that allow you to manipulate elements of a list.

# Creating a Python list
my_list = [3, 5, 2, 8, 1]

# Slicing a list
sub_list = my_list[1:4]  # The sub_list becomes [5, 2, 8]

# Concatenation of two lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated_list = list1 + list2  # The concatenated_list becomes [1, 2, 3, 4, 5, 6]

# Repetition of a list
repeated_list = list1 * 2  # The repeated_list becomes [1, 2, 3, 1, 2, 3]

# List comprehension
squared_list = [x ** 2 for x in my_list]  # The squared_list becomes [9, 25, 4, 64, 1]

# Using built-in methods
my_list.append(4)  # The list becomes [3, 5, 2, 8, 1, 4]
my_list.pop()      # The list becomes [3, 5, 2, 8, 1]
my_list.sort()     # The list becomes [1, 2, 3, 5, 8]
List Operations in Python

Python Sets: Sets are optimized for performing set-related operations like union, intersection, difference, and checking membership using hash functions to find elements quickly. Since they are unordered and lack indexing, set operations differ from list-based ones.

# Creating Python sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

# Union operation
union_set = set1.union(set2)  # The union_set becomes {1, 2, 3, 4, 5, 6, 7, 8}

# Intersection operation
intersection_set = set1.intersection(set2)  # The intersection_set becomes {4, 5}

# Difference operation
difference_set = set1.difference(set2)  # The difference_set becomes {1, 2, 3}

# Checking membership
if 3 in set1:
    print("3 is a member of set1")
# Output: 3 is a member of set1
Set operations in Python

How Do You Choose The Right Data Structure?

When working with Python, it’s essential to select the most suitable data structure for your specific task. In this section, we will discuss the best scenarios for using sets and lists, along with their unique advantages.

Let’s get into it.

Use Cases for Sets

Sets offer several advantages that make them the ideal choice for certain tasks:

Uniqueness: If you need to store a collection of unique elements, sets are the way to go. Sets automatically eliminate duplicates, ensuring that each element in the set is distinct.

Membership tests: Sets provide faster membership tests compared to lists. Due to their underlying hash table implementation and the use of hash functions, sets allow for highly efficient lookups based on hash values.

Set operations: Sets support operations such as union, intersection, difference, and symmetric difference that can be useful in many algorithms, data processing tasks, and data science applications.

Use Cases for Lists

Lists are better suited for the following scenarios:

Ordered data: Lists maintain the order of elements, making them suitable for tasks that require respecting the sequence of items, such as processing data in the order it was created or when support indexing is needed.

Mutable data: Lists are mutable, allowing you to add, remove, or modify a specific element as needed. This flexibility makes lists suitable for tasks that involve changing the content of the collection or when working with nested data structures, such as lists of lists or dictionaries.

Non-unique elements: Unlike sets, lists can store duplicate elements, making them appropriate for situations where the frequency of items matters, such as counting occurrences or maintaining the order of duplicate values.

Check out the below to show to further your learning.

APerformance Comparison Between Sets and Lists

In this section, we will compare the performance of Python sets and lists in terms of time complexity and memory usage, which is essential when working with large data structures or when optimizing code for efficiency.

Time Complexity

When it comes to time complexity, sets and lists have different strengths and weaknesses depending on the operations you perform due to their underlying implementation.

  1. Searching: Sets use hash lookups and hash functions, which makes searching for an item significantly faster compared to lists. For example, searching through 100,000 items takes 49.663 seconds with a list, but only 0.007 seconds with a set, as it takes advantage of the hash value for quick access.

  2. Iteration: Lists are slightly faster than sets when it comes to iterating over the items. This is because sets require additional operations to ensure uniqueness, while lists maintain a simple ordered collection with direct indexing.

Memory Usage

Sets typically consume more memory than lists because they need to maintain a hash table to ensure the uniqueness of items, which comes at the cost of increased memory consumption.

Lists only store the elements sequentially, leading to lower memory consumption, making them a more memory-efficient choice when handling large collections of data.

import time
import random

# Generating a large list and set with 100,000 random integers
large_list = [random.randint(1, 1_000_000) for _ in range(100_000)]
large_set = set(large_list)

# Searching for an item in the list and set
search_value = random.randint(1, 1_000_000)

# Measuring the time it takes to search for the item in the list
start_time = time.time()
result = search_value in large_list
end_time = time.time()
list_search_time = end_time - start_time
print(f"List search time: {list_search_time:.6f} seconds")

# Measuring the time it takes to search for the item in the set
start_time = time.time()
result = search_value in large_set
end_time = time.time()
set_search_time = end_time - start_time
print(f"Set search time: {set_search_time:.6f} seconds")

# Iterating over the list and set
# Measuring the time it takes to iterate over the list
start_time = time.time()
for item in large_list:
    pass
end_time = time.time()
list_iter_time = end_time - start_time
print(f"List iteration time: {list_iter_time:.6f} seconds")

# Measuring the time it takes to iterate over the set
start_time = time.time()
for item in large_set:
    pass
end_time = time.time()
set_iter_time = end_time - start_time
print(f"Set iteration time: {set_iter_time:.6f} seconds")

The provided code showcases the performance comparison between Python sets and lists in terms of time complexity for searching and iteration.

It generates a large list and set of random integers, enclosed by curly brackets (also called curly braces).

It then measures the time taken to search for a specific item, using a single argument in both the list and set, and measures the time taken to iterate through all elements in the list and set.

Time Complexity Output Comparison

The output illustrates the performance differences between Python lists and sets for search and iteration, which stem from their underlying implementation.

The search operation is faster in sets (0.000000 seconds) than in lists (0.002999 seconds) due to the use of hash functions to compute hash values for efficient lookups. However, iterating over a list (0.007995 seconds) is slightly faster than iterating over a set (0.017989 seconds) since sets require additional operations to ensure uniqueness.

Common Operations and Methods

Both sets and lists in Python have various operations and methods, each optimized for specific tasks and data manipulation. Some of these methods are listed below:

Set Method

Set methods perform operations that are similar to mathematical operations and are powerful tools for handling unique values in a collection.

  • add(element): Adds an element to the set if it is not already present.

  • remove(element): Removes the specified element from the set; raises an error if the element is not found.

  • discard(element): Removes the specified element from the set if it is present. No error is raised if the element is not found.

  • union(set2): Returns a new set containing all elements from the original set and set2, effectively performing a set operation.

  • intersection(set2): Returns a new set containing elements common to both the original set and set2.

  • difference(set2): Returns a new set containing elements in the original set but not in set2.

  • symmetric_difference(set2): Returns a new set containing elements in either the original set or set2, but not in both.

List Method

List methods provide various ways to manipulate data.

  • append(element): Adds an element to the end of the list.

  • extend(iterable): Appends all elements from the iterable (e.g., another list) to the end of the list.

  • insert(index, element): Inserts an element at the specified index.

  • remove(element): Removes the first occurrence of the specified element in the list; raises an error if the element is not present.

  • pop(index): Removes and returns the element at the specified index. If no index is given, it removes the last element.

  • index(element): Returns the index of the first occurrence of the specified element in the list.

  • count(element): Returns the number of occurrences of the specified element in the list.

  • sort(): Sorts the list in ascending order by default; for descending order, use the reverse=True parameter.

  • reverse(): Reverses the order of the elements in the list.

By using these Python set and list methods, you can effectively manipulate your data and solve various problems in Python programming, data science, and other applications.

Our Final Say

When choosing between Python lists and sets for your data structure, consider using lists when you need an ordered collection of items, want to preserve duplicate elements, and require the ability to access elements by index.

Opt for sets when the uniqueness of elements is essential, the order of elements is not important, and faster membership testing is preferred. While lists excel in iteration, sets provide more efficient containment checks.

Your choice ultimately depends on your project’s requirements, as each data structure offers its own set of benefits and limitations, making them powerful tools for tackling various tasks in Python programming. Enjoy!

Related Posts