5 ways to convert List to Set in Python

Python Set

In this post, we are going to understand 5 ways to convert List to Set in Python with code examples. We will explore very simple ways to convert Python List to set.

1. Set constructor to convert List to Set


To convert a LIST into a SET we can use inbuilt the set() constructor. We pass a list as an argument to set the constructor.

The main difference between SET and List is that set is an unordered, unique collection that does not allow duplicate. So while converting the List to SET all the duplicate items of the list will be removed. This is an easy method to convert List to Set

Program Example

list_num =[12,15,12,20,25,25,15]
print(f'original List:\n{list_num}\n')
      
myset = set(list_num)
print(f'result set :\n{myset }')

output

original List:
[12, 15, 12, 20, 25, 25, 15]

result set :
{25, 12, 20, 15}

2.Convert List of string to Set


In this example, we are converting a List of strings to SET. As you can see from the output, all the duplicate has been removed.

In this example, we are performing operations on a list of numbers to SET. As you can see output all the duplicate has been removed.

Program Example

 
list_animal =['dog','cat','mouse','snake','fox','tiger','lion']
print(f'original list:\n{list_animal}\n')
      
list_to_set = set(list_animal)
print(f'set from list:\n{list_to_set}')
 

Output

original list:
['dog', 'cat', 'mouse', 'snake', 'fox', 'tiger', 'lion']

set from list:
{'lion', 'snake', 'mouse', 'tiger', 'cat', 'dog', 'fox'}

3. Convert a List of mixed dataype to Set


In this example, we are performing operation a List of mixed datatype to SET. As you can see output all the duplicates have been removed while converting.

Program Example

 
list_orignal =[12,15,12,20,25,25,15,'at','at','on',23.5,20.4,23.5]
print(f'original List:\n{list_orignal}\n')
      
list_to_set = set(list_orignal)
print(f'List to Set  :\n{list_to_set}')

Output

original List:
[12, 15, 12, 20, 25, 25, 15, 'at', 'at', 'on', 23.5, 20.4, 23.5]

List to Set  :
{'at', 'on', 20.4, 12, 15, 20, 23.5, 25}

4.Convert List to Set Preserve order


The simple ways to convert a list to set by Preserve order. The steps are here:

  • convert list to the dictionary using dict.fromkeys() method that removes all duplicates and dictionary to list
  • Convert dictionary to list using list() constructor.

Program Example

list_animal =['dog','cat','cat','snake',13,14,15]
list_to_dict = dict.fromkeys(list_animal)
print('list to dict:\n',list_to_dict)

result_list = list(list_to_dict)
print('\n',result_list)

Output

list to dict:
 {'dog': None, 'cat': None, 'snake': None, 13: None, 14: None, 15: None}

 ['dog', 'cat', 'snake', 13, 14, 15]

5. Convert lists of list to Set


We have defined a custom function lists_to_Set to convert a list of lists to Set.

Program Example

result_list = [] 
def lists_to_Set(lists_to_set): 
    for val in lists_to_set: 
        if type(val) == list: 
            lists_to_Set(val) 
        else: 
            result.append(val)
    return set(result_list)


lists_to_set = [[10, 17,17], [17, 15, 34, 34], [12,16,89,90]]
print(lists_to_Set(lists_to_set))

Output

{34, 10, 12, 15, 16, 17, 89, 90}

Summary

In this post we have learned How to convert List to Set in Python with code example.