Convert a List to a Set in Python

  • Post category:List

Converting a list to a set is a common task in Python programming. In this blog post, we’ll explore different code examples that demonstrate how to convert a list to a set efficiently.

Example 1:

numbers = [1, 2, 2, 3, 4, 4, 5]
numbers_set = set(numbers)
print(numbers_set)

Output: {1, 2, 3, 4, 5}

Example 2:

fruits = ['apple', 'banana', 'orange', 'banana', 'kiwi', 'apple']
fruits_set = set(fruits)
print(fruits_set)

Output: {‘apple’, ‘banana’, ‘kiwi’, ‘orange’}