How to convert a list to a set in Python

Python have different built-in datatypes which are used to store collection of data items. Set is one of them to store an unordered collection of unique items. It is written in curly brackets. Unlike lists, a set cannot have repeating elements. Suppose you’re given a list of items and you are asked to write a python program to convert this list into set. In this tutorial, we will discuss and learn how to convert a list to a set in Python. If you want to learn more about Python Programming, visit Python Programming Tutorials.

Before diving deep into the topic, let first discuss the difference between list and set. Both set and lists are the collection of items. But list is an ordered collection of items which can be modified or deleted. On the other hand, elements in a set are unordered and cannot be modified. Another main difference between List and Set is that List allows duplicate elements whereas set doesn’t. In most of the applications, you have to deal with huge amounts of data such as stock market analysis. Redundant records can affect your analysis . You need to remove the duplicates. All elements in a set are unique. Thus, you can convert a list to a set in order to remove duplicate items.

There are different ways by which you can convert a list to a set. Three of them are listed as under:

  • Using Set() Function
  • Using Custom Function
  • Using Dictionary Function (dict.fromkeys())

Using set() Function to convert a list to a set

This most simplest and straightforward method is to use the set() constructor and pass the list as an argument. The set() constructor converts the list to a set. Lets consider an example in which you have a list of students and you want to print the unique names or store them. The set() function remove the duplicates and store them.

#create a list
names_list= ['Joe', 'Jen', 'John', 'Jen', 'Jen', 'Alexander']
#convert the list using set
s = set(names_list)
#display the set
{'Alexander', 'John', 'Jen', 'Joe'}
<class 'set'>

Here, the ‘type’ command is used to check the datatype of the variable. As ‘s’ is a set, therefore the type of ‘s’ is set.

Using Custom Function for conversion of a list to a set

You can also create your own customized function for the conversion of a list to set. In this function, you can also perform some other tasks. Suppose you’re an owner of a store. You have a list of names of persons that visited your store today. You want to check how many unique persons visited your store to perform a detailed analysis in order to increase revenue.

Here, List “Names” consists of names of all persons that visited your store today. Now, create a function that takes list of customers as an input and add each element into a set ‘s’ using a for loop. On each iteration, an item is checked if it is already present, the item is ignored otherwise added into a set. At the end, you can print the number of customers by printing the length of a set.

def conversion(list):
    s = set()
    for x in list:
    return s

Names = ['John', 'Joe', 'Jane', 'Harry', 'Leo', 'Jen', 'John', 'Jen', 'Jen', 'Alexander', 'Leo']
print("Names of unique customers are: ",n)
print('Total number of unique customers are: ',len(n))
Names of unique customers are:  {'Leo', 'Jane', 'Harry', 'Jen', 'Alexander', 'John', 'Joe'}
Total number of unique customers are:  7

Using Dictionary function

In the above two methods, you have observed that when the list is converted to a set, the order of items is changed. You can place these items into the set in an ascending order using dictionaries. Dictionary has a fromkeys() function which convert the list into a dictionary in such a way that all items of list become keys of the dictionary. As the key should be unique, therefore all duplicates are removed. Then a list is created again which is then converted into a set ‘s’ using a set function. The code is given below.

list_1 = ['John', 'John', 'Ana', 2, 2, 3, 4]
x = list(dict.fromkeys(list_1))
s = set(x)

On executing the above code, you’ll get following output:

{2, 3, 4, 'Ana', 'John'}

From the output, you will observe that all the elements are placed in ascending order.

There are some other methods also such as set comprehension to convert list into a set in a single line. It is similar to list comprehension. For practice, try it on your own. If you’ve any questions about this article, Let us know your valuable feedback in the comments.

Leave a Comment

Your email address will not be published.