Python - How To Convert List to Set in Python

ID : 276

viewed : 203

Tags : PythonPython SetPython List

vote vote


This tutorial demonstrates how to convert a list into a set in Python.

Difference Between a List and a Set

Lists and sets are standard Python data types that store values in sequence.

A Python List stores comma-separated values between square brackets. It’s mutable, which means that its items can be modified or changed. The thing about the Python List is that the items do not need to be of the same type.

Below is an example of what a Python List looks like:

list_1 = [1, 2, 3] list_2 = ['Milk', 'Bread', 'Eggs'] # List with multiple data types list_3 = ['Chocolate', 15.25, 200] 

Python sets are an unordered list of unique elements that may have different data types. The object stores comma-separated values and is encapsulated in curly braces {}. It is iterable and mutable, yet it only stores immutable objects; this means that a set itself can be modified — we can add or remove elements; however, the elements inside the set must not change. We cannot modify or access the elements inside a set using slicing or indexing.

Here’s an example of what a Python Set looks like:

set_1 = {1, 2, 3} set_2 = {'Milk', 'Bread', 'Eggs'} # Sets with multiple data types set_3 = {17.0, 'Hello World', (10, 20, 30)} 

Now, we know the differences between a Python List and a Set, let’s dive into converting a List into a Set.

Using the set() Method to Convert the List to Set in Python

The simplest way to convert a list to a set is by using Python’s built-in method, the set() method. Converting a List with duplicate values will be automatically removed by the set() method and will retain the same unique elements as the initial List. The set() method only accepts one parameter as an argument:

*iterable (optional) - a sequence (string, list, tuple, etc.) or collections of elements

Here’s the syntax:


The set() method returns:

  • an empty set, if no arguments is passed.
  • a constructed set

Example 1: Convert a Python List with Unique Values

# initialize a list _list = [5, 4, 3, 1, 6, 2]  # convert list to set converted = set(_list)  print(converted) 


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

Example 2: Convert List with Duplicate Values

# initialize a list grocery_list = ["milk", "bread", "eggs", "bread", "hotdogs", "eggs"]  # convert list to set converted = set(grocery_list)  print(converted) 


{'eggs', 'bread', 'milk', 'hotdogs'} 

As you can see, the converted variable now only contains unique values.

Example 3: Convert List with Mixed Data Types

# initialize a list with mixed data types grocery_list = ["milk", "bread", "eggs", ("Ham and Cheese", 2000), 19.43]  # convert the grocery_list to set grocery_set = set(grocery_list)  # display the converted grocery_list print(grocery_set)  


{'milk', 'bread', 'eggs', ('Ham and Cheese', 2000), 19.43} 

Python sets provide an array of methods you can choose from depending on your use-case, and not just convert a list into a set. There are set methods used to perform mathematical operations: union, intersection, difference, and symmetric difference. We can achieve this by using some defined Python set operators and methods.

  • Related HOW TO?