How to Convert a List Into a Set in Python

You can convert a list into a set in Python.
Image Credit: Poike/iStock/GettyImages

Lists and sets in Python both contain groups of items, but there are important differences between the two. In particular, sets are useful for performing a wide range of operations such as unions and intersections, while lists have fewer mathematical operations but are useful for searching for specific items – especially with duplicates – and organizing data. For many reasons, you might need to convert a list to a set in Python, and there is a simple function you can use to do this.

Python Lists and Sets Explained

The most important difference between a list and a set in Python is that a list can contain duplicate elements, while a set only contains unique elements. For example, if you have a list of tests scores defined as "scores=[14, 20, 13, 20, 15]," the list tells you every value, but if you convert it into a set, it removes the duplicate and leaves {14, 20, 13, 15}.

Another difference is that square brackets are used for lists, while curly brackets are used for sets. Things are complicated a little by the fact that a Python dictionary also uses curly brackets, but there each item consists of the value itself and a "key" separated by a colon.

It's important to remember that you can use strings, integers, floats or other objects as part of a set or a list, and these can also be mixed in the same set or list. So, just like you can have a set of numbers in Python, you can also construct a Python set of strings.

Convert a List Into a Set

The process for converting a list into a set in Python is straightforward. First, you define the list in a way that makes sense for the information contained in it. For example, say you want to define a list of names, you might write names=['Lee', 'Mike', 'Jamie', 'Mike', 'Sarah'] noting the square brackets, quotation marks around the strings (the names), and commas separating each entry.

To convert this list into a set, you use the command set(), where you put the name of the list you want to convert between the parentheses. So, in this case, you can write set(names) to convert the names in the list into a set, and you can assign it a variable such as "uniquenames" and display the result using print(uniquenames), which is functionally equivalent to print(set(names)) but neater. You confirm this has been done correctly by noting that the result uses curly brackets, and "Mike" is only included once.

The whole code looks like this:

names=['Lee', 'Mike','Jamie','Mike','Sarah']

uniquenames=set(names)

print('Unique names: ', uniquenames)

In this case, "Unique names:" has been added to the output for presentation purposes.

Python: Set to List Conversion

You can also run the same conversion in the opposite direction, and the process is basically the same, except you use the list() command on a set rather than the other way around. This means you need a set to start with, which you can define easily by picking a variable such as "big_set" and then adding the contents, separated by commas and in curly brackets. You could write big_set={1, 3, 4, 5, 7, 8, 9, 11, 12, 14, 15, 17} to define a set of integers or any other values.

To convert this into a list, you define a variable (say, "big_list") and use the list() command, so you type something like big_list=list(big_set) and then print(big_list) to display the result. So the full code is:

big_set={1, 3, 4, 5, 7, 8, 9, 11, 12, 14, 15, 17}

big_list=list(big_set)

print(big_list)

This case doesn't give the list a name, so it only returns the list. Note that if you convert a list to a set and then back again, you lose any repeated values in the first conversion, and they don't come back.

references