Open In App

Sets vs. Lists - Python

Last Updated : 10 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, both sets and lists are used to store collections of elements but they have key differences that make them suitable for different use cases. The main difference is that lists are ordered, meaning the order of elements is preserved and they can be accessed by their index while sets are unordered meaning they do not maintain the order of elements and automatically ensure all elements are unique. This article will explore the differences between sets and lists, including their properties, performance, and typical use cases.

Overview of Sets

A set is an unordered collection of unique elements. Sets are defined using curly braces {} or the set() function.

Example:

Python
# Creating a set
s1 = {1, 2, 3, 4}
s2 = set([4, 5, 6, 7])

print(s1)       
print(s2)  

Output
{1, 2, 3, 4}
{4, 5, 6, 7}

Overview of Lists

A list is an ordered collection of elements that can contain duplicates. Lists are defined using square brackets [].

Example:

Python
# Creating a list
a = [1, 2, 3, 4]
b = list([4, 5, 6, 7])

print(a)       
print(b)  

Output
[1, 2, 3, 4]
[4, 5, 6, 7]

Key Differences: Sets vs. Lists in Python

FeatureListsSets
OrderOrdered, elements are indexed and can be accessed via their index.Unordered, elements do not have a specific position.
UniquenessCan contain duplicate elements.Contain only unique elements, automatically removing duplicates.
MutabilityMutable, elements can be added, removed, or changed.Mutable, elements can be added or removed, but individual elements cannot be changed.
PerformanceSlower for membership tests (O(n) complexity) as it may need to check each element.Faster for membership tests (O(1) average complexity) due to hash table implementation.
OperationsSupport slicing, indexing, and various sequence operations.Support mathematical operations like union, intersection, and difference.
Use Cases- Use when you need to maintain the order of elements.
- Suitable for tasks where elements are accessed by index.
- Ideal for collections of items where duplicates are allowed.
- Use when you need to ensure all elements are unique.
- Suitable for membership testing and eliminating duplicates.
- Ideal for mathematical set operations like union, intersection, and difference.

Key Operations in Set & List

Here’s a basic example demonstrating operations on sets and lists in Python:

Sets:

Python
# Creating a set
s1 = {"apple", "banana", "cherry"}

# Adding an item
s1.add("orange")

# Removing an item (will not raise error if item is not present)
s1.discard("banana")

# Union of two sets
s2 = {"carrot", "potato", "onion"}
s3 = s1.union(s2)

# Intersection of two sets
s4 = s1.intersection(s2)

# Print all sets
print(s1)
print(s2)
print("Union:", s3)
print("Intersection:", s4)

Output
{'apple', 'cherry', 'orange'}
{'potato', 'carrot', 'onion'}
Union: {'potato', 'cherry', 'onion', 'apple', 'carrot', 'orange'}
Intersection: set()

Lists:

  • append(): Adds an element to the end.
  • remove(): Removes a specific element by value.
  • Indexing ([0]): Accesses elements at a specific position.
  • Slicing ([start:end]): Extracts a sublist.
Python
# Creating a list
a = [1, 2, 3, 4, 5]

# Adding an item (to the end of the list)
a.append(6)

# Removing an item by value
a.remove(3)

# Accessing an item by index
val = a[0]

# Slicing the list
b = a[1:4]

# Print all lists
print(a)
print("First item:", val)
print("Sublist (sliced):", b)

Output
[1, 2, 4, 5, 6]
First item: 1
Sublist (sliced): [2, 4, 5]



Next Article

Similar Reads