Convert a List to a Set in Python

To convert a list to a set in Python, you can use the built-in set() function. This transformation removes duplicate elements and stores the unique values in an unordered collection. Let’s explore different examples to achieve this.


Examples

1. Converting a List to a Set Using set()

The simplest way to convert a list to a set is by passing the list as an argument to the set() constructor.

</>
Copy
# Creating a list with duplicate elements
numbers = [1, 2, 2, 3, 4, 4, 5]

# Converting the list to a set
unique_numbers = set(numbers)

# Printing the resulting set
print("Converted Set:", unique_numbers)

Explanation:

Here, we define a list numbers that contains duplicate elements. We use the set() function to remove duplicates and create a new set unique_numbers. The set stores only distinct values.

Output:

Converted Set: {1, 2, 3, 4, 5}

2. Converting a List of Strings to a Set

You can also convert a list of strings into a set, removing duplicate words.

</>
Copy
# Creating a list of strings
fruits = ["apple", "banana", "apple", "orange", "banana"]

# Converting the list to a set
unique_fruits = set(fruits)

# Printing the resulting set
print("Converted Set:", unique_fruits)

Explanation:

The list fruits contains repeated elements. When we pass it to the set() function, it removes duplicates and stores only unique values in the unique_fruits set.

Output:

Converted Set: {'apple', 'banana', 'orange'}

3. Converting a List with Mixed Data Types

A list containing different data types (integers, strings, booleans) can also be converted into a set.

</>
Copy
# Creating a list with mixed data types
mixed_list = [1, "hello", 2, "hello", True, 1, False, 2]

# Converting to a set
unique_elements = set(mixed_list)

# Printing the resulting set
print("Converted Set:", unique_elements)

Explanation:

The list mixed_list contains integers, strings, and boolean values. When converted to a set, it keeps only unique elements. Note that True is treated as 1 and False as 0, so they are not counted separately.

Output:

Converted Set: {False, 1, 2, 'hello'}

4. Removing Case-Sensitive Duplicates from a List

When dealing with a list of strings, you may want to convert all elements to lowercase before converting them to a set.

</>
Copy
# Creating a list with case-sensitive duplicates
words = ["Hello", "world", "hello", "WORLD", "Python"]

# Converting to lowercase before making a set
unique_words = set(word.lower() for word in words)

# Printing the resulting set
print("Converted Set:", unique_words)

Explanation:

The list words contains elements with different letter cases. By converting each word to lowercase using word.lower() inside a set comprehension, we ensure case-insensitive uniqueness.

Output:

Converted Set: {'hello', 'python', 'world'}

5. Converting a List of Tuples to a Set

Lists containing tuples can also be converted into sets, preserving unique tuple elements.

</>
Copy
# Creating a list of tuples
pairs = [(1, 2), (3, 4), (1, 2), (5, 6)]

# Converting to a set
unique_pairs = set(pairs)

# Printing the resulting set
print("Converted Set:", unique_pairs)

Explanation:

The list pairs contains duplicate tuples. The set() function removes duplicates, ensuring that only unique tuple pairs remain.

Output:

Converted Set: {(1, 2), (3, 4), (5, 6)}

Conclusion

In Python, converting a list to a set is a simple way to remove duplicates and store unique elements. The set() function efficiently performs this transformation while preserving only distinct values.