Convert a list to a set in Python
This tutorial demonstrates how to convert a list to a set in Python.
Difference between List and Set
Lists and sets are standard Python data types that store values in sequence.
Python list stores comma separated values between square brackets. It is mutable, which means its items can be modified or changed. The thing about Python list is that the items do not need to be of the same type.
Here is an example of a Python list:
list_1 = [1, 2, 3]
list_2 = ["Milk", "Bread", "Eggs"]
# List with multiple data types
list_3 = ["Chocolate", 15.25, 200]
A Python set is an unordered list of unique elements that may have different data types. The object stores comma-separated values and is enclosed in curly braces {}
. It is iterable and mutable, but it only stores immutable objects; this means that the set itself can be modified—we can add or remove elements; however, the elements inside the set must not be changed. We cannot use slicing or indexing to modify or access elements inside a set.
Here is an example of a Python set:
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 that we know the difference between Python lists and sets, let’s dive deeper into converting lists to sets.
set()
Convert a list to a set using the method in Python
The simplest way to convert a list into a set is to use Python's built-in method, set()
the set method. set()
The set method will automatically remove lists with duplicate values and will keep the same unique elements as the initial list. set()
The set method accepts only one argument as a parameter:
*iterable (optional) – a sequence (string, list, tuple, etc.) or a collection of elements
Here is the syntax:
set(iterable)
set()
Method returns:
- An empty set if no arguments were passed.
- A constructed collection
Example 1: Converting 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)
Output:
{1, 2, 3, 4, 5, 6}
Example 2: Converting a list with repeated values
# initialize a list
grocery_list = ["milk", "bread", "eggs", "bread", "hotdogs", "eggs"]
# convert list to set
converted = set(grocery_list)
print(converted)
Output:
{'eggs', 'bread', 'milk', 'hotdogs'}
As you can see, converted
the variable now contains only unique values.
Example 3: Converting a 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)
Output:
{'milk', 'bread', 'eggs', ('Ham and Cheese', 2000), 19.43}
Python collections provide a range of methods that you can choose from based on your use case, beyond just converting a list to a set. There are collection methods for performing mathematical operations: union, intersection, difference, and symmetric difference. We can do this by using some of the defined Python collection operators and methods.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert a list to lowercase in Python
Publish Date:2025/05/08 Views:112 Category:Python
-
Lists can be used to store multiple items in a single variable. In Python, we can create a list of strings by enclosing the different elements in the list in single or double quotes. This tutorial demonstrates how to convert a list of strin
Convert hex to bytes in Python
Publish Date:2025/05/08 Views:101 Category:Python
-
Hexadecimal, often abbreviated to hex, uses 16 symbols (0-9, a-f) to represent values, in contrast to the decimal system's 10. For example, 1000 in decimal is 3E8 in hexadecimal. Being proficient in dealing with hexadecimal is essential for
How to Convert Integer to Binary in Python
Publish Date:2025/05/08 Views:130 Category:Python
-
This tutorial explains how to convert an integer to binary in Python. This tutorial also lists some sample codes to illustrate different ways of converting from int to binary in Python. bin() Convert Int to Binary in Python using In Python,
How to convert an integer to bytes
Publish Date:2025/05/08 Views:77 Category:Python
-
Converting an integer int to a byte bytes is the inverse of bytes converting a byte to an integer . Most of the to methods int described in this article are the inverse of the to methods. int bytes bytes int Generic method for converting in
How to convert bytes to int in Python
Publish Date:2025/05/08 Views:111 Category:Python
-
Bytes The data type has a numerical range of 0~255 (0x00~0xFF). A byte has 8 bits of data, that's why its maximum value is 0xFF. In some cases, you need to convert a byte or byte array to an integer for further data processing. Let's see ho
How to Convert an Integer to a String in Python
Publish Date:2025/05/08 Views:69 Category:Python
-
This article will show you different ways to convert an integer to a string using Python code, such as str() the function and the f format method. str() Convert integer to string in Python using function We can use the inbuilt function in P
Implementing Curl command in Python using requests module
Publish Date:2025/05/07 Views:98 Category:Python
-
requests This article will discuss and implement different curl commands using the module in Python . requests Installing modules in Python Python provides us with requests the module to execute curl command. Install it in Python 3 using Pi
Completely uninstall Miniconda
Publish Date:2025/05/07 Views:76 Category:Python
-
Miniconda is a small, portable, and minimally bootable version of the Anaconda installer. It contains only Python, conda its dependencies, and some other useful packages such as zlib and pip . Miniconda The package can be supported and inst
Sorting a collection in Python
Publish Date:2025/05/07 Views:106 Category:Python
-
A set is an unordered and unindexed collection with no duplicate elements. Sets are one of the four built-in data types available in Python and are written using curly braces. Given that sets are unordered, it is not possible to sort the va