How To Convert List To Set In Python

A list is a data structure which stores non-homogeneous data items. It is also referred to as an abstract data type. A list is mutable and ordered in nature. A list allows duplicate data items or elements. In python, a list is created with variable name and square braces i.e. listname=[]. A syntax and example to create a  list is given below.

 Syntax:       

                listname = [item1, item2, item3,…..itemN]

Example:

               Employee=[‘Jhon’, ‘Male’,‘NewYork’, 37, 55000 ]

An above example shows creating a list named as Employee which contains  names of employees. A python code to create a list is demonstrated below.

Python Code to create a List:

# Python code to create a list
Employee=['Marry', 'Cherry','Jhonson', 'Marry', 'Sunny' ]      # Here Employee list is created
print('List items are: ',Employee)      # It display list items

Output:

List items are:  ['Marry', 'Cherry', 'Jhonson', 'Marry', 'Sunny']

Code Explanation:

  • Comment line tells the goal of the program to programmers.
  • A list named Employee is created which contains the name, gender age and salary.
  • Elements of the list are displayed.

Why Is There A Need To Convert List to Set  In Python

In python, it is required to convert lists to sets for several reasons. Here, some reasons are presented.

  • Filter unique data items of list: Python list allows duplicate data items. But the set does not allow duplicate elements. When we want to retrieve or access unique elements from the list then we need to convert the list to set.
  • Perform set operations: If we want to perform set operations such as union, intersection on a list elements then we are required to convert the list to set.
  • Random access of list data: If we want to access data items in random order from the list then conversion is required.

Approaches: How to Convert List to Set

To convert a list to set, several approaches are available. There are some approaches listed below. 

  • Approach-1: Using set() function
  • Approach-2: Using loop and set add() function
  • Approach-3: Using set comprehension
  • Approach-4: Using dict.fromkey() function

Approach-1: Using set() method

A set() is an inbuilt function of python which converts list to set. It takes a list as an argument and returns set elements . A python code to convert a list to set using set() function demonstrated below. 

Code:

# Python code to create a list
Employee=['Marry', 'Cherry','Jhonson', 'Marry', 'Sunny' ]      # Here Employee list is created
Emp=set(Employee)              # It convert list Employee to set Emp
print('List items are: ',Employee)      # It display list items
print('Set items are: ',Emp)    # It display set items

Output:

List items are:  ['Marry', 'Cherry', 'Jhonson', 'Marry', 'Sunny']
Set items are:  {'Cherry', 'Jhonson', 'Marry', 'Sunny'}

 Code Explanation:

  • Comment line tells the goal of the program to programmers.
  • A list  named Employee is created which contains the names of employees.
  • A list is converted to set using set() function
  • A list and set items are displayed. Set items are unordered and unique.

Approach-2: Using a loop and set add() function

To convert a list to string, another method is use of for loop and add() function of set. In this method, every element of the list is accessed using a for loop and added to the empty set using add() function. A python code to convert a list to set using loop and add() function demonstrated below. 

# Python code to convert a list to string
Employee=['Marry', 'Cherry','Jhonson', 'Marry', 'Sunny' ]      # Here Employee list is created
Emp=set()     # Empty set named Emp is created using set() function
for i in Employee:  # Each item of list is accessed here
  Emp.add(i)    # Item of list is added to the set using add() function    
print('List items are: ',Employee)      # It display list items
print('Set items are: ',Emp)    # It display set items

Output:

List items are:  ['Marry', 'Cherry', 'Jhonson', 'Marry', 'Sunny']
Set items are:  {'Cherry', 'Jhonson', 'Marry', 'Sunny'}

Code Explanation:

  • Comment line tells the goal of the program to programmers.
  • A list  named Employee is created which contains the names of employees.
  • An empty set is created using set() function
  • Each list item is accessed and added to set using add() function
  • A list and set items are displayed. Set items are unordered and unique.

Approach-3: Using  Set Comprehension

With the help of the set comprehension method, we can also convert a list to set. Set comprehension allows you to create a set using a for loop in single line code. It is a short way to create a set. A python code to convert a list to set using list comprehension demonstrated below. 

# Python code to convert a list to string
Employee=['Marry', 'Cherry','Jhonson', 'Marry', 'Sunny' ]      # Here Employee list is created
Emp={i for i in Employee} # Item of list is assigned to the set using list comprehension                         
print('List items are: ',Employee)      # It display list items
print('Set items are: ',Emp)    # It display set items

Output:

List items are:  ['Marry', 'Cherry', 'Jhonson', 'Marry', 'Sunny']
Set items are:  {'Cherry', 'Jhonson', 'Marry', 'Sunny'}

Code Explanation:

  • Comment line tells the goal of the program to programmers.
  • A list  named Employee is created which contains the names of employees.
  • Each list item is accessed and added to the set using list comprehension.
  • A list and set items are displayed. Set items are unordered and unique.

Approach-4: Using Dict.fromkey() function

Dict.fromkey() function offers another way to convert a list to set. This function first creates a dictionary of specified keys and their values. Then it converts the dictionary to a list. Finally a list is converted to a set using set() function.  A python code to convert a list to set using Dict.fromkey() function demonstrated below. 

# Python code to convert a list to string
Employee=['Marry', 'Cherry','Jhonson', 'Marry', 'Sunny' ]      # Here Employee list is created
Emp=list(dict.fromkeys(Employee)) # It convert dictionary to list
EmpSet=set(Emp)             # It convert Emp list ot EmpSet using set() function
print('List items are: ',Employee)      # It display list items
print('Set items are: ',Emp)    # It display set items

Output:

List items are:  ['Marry', 'Cherry', 'Jhonson', 'Marry', 'Sunny']
Set items are:  ['Marry', 'Cherry', 'Jhonson', 'Sunny']

Code Explanation:

  • Comment line tells the goal of the program to programmers.
  • A list  named Employee is created which contains the names of employees.
  • A list is created using dict.fromkeys() function. Then a list is converted to a set using set() function.
  • A list and set items are displayed. Set items are unordered and unique

Best Approach- Set Comprehension

The best approach to change a list to a set in Python is Set Comprehension. It is the best approach because:

  • The Set Comprehension approach provides a short and very convenient way to convert a list to set.
  • It does not use any in-built function such as set() and add() functions.     
  • Set Comprehension short length of code.
  • Its time complexity is also less compared to other methods.

Sample problems related to convert list to set in Python

Sample Problem-1: Using Approach-1

Problem Definition: Create a python code to demonstrate how to convert a list to set using set() function. For this, create a list named PeopleContact which contains Contact numbers of people.  Then convert the created list to set using set() function.

Solution (Python Code):

# Python code to convert a list to set using approach-1
PeopleContact=[9993344556,9630989900,9993877389,883456128,9993877389,7744536790 ]      # Here PeopleContact list is created
PC=set(PeopleContact)              # It convert list PeopleContact to set PC
print('List items are: ',PeopleContact)      # It display list items
print('Set items are: ',PC)    # It display set items

Output:

List items are:  [9993344556, 9630989900, 9993877389, 883456128, 9993877389, 7744536790]
Set items are:  {883456128, 9993344556, 9993877389, 9630989900, 7744536790}

  Code Explanation:

  • Comment line tells the goal of the program to programmers.
  • A list  named PeopleContact is created which contains the contact numbers of people.
  • A list is converted to set using set() function
  • A list and set items are displayed. Set items are unordered and unique.

Sample Problem-2:Using Approach-2

Consider a list named PeopleContact which contains Contact numbers of people. Create a python code to demonstrate the display of a unique element of a list using set() function.

# Python code to convert a list to set using approach-2

PeopleContact=[9993344556,9630989900,9993877389,883456128,9993877389,7744536790 ]      # Here PeopleContact list is created
for i in PeopleContact:                     # Each item of list is accessed here
  PC.add(i)                        # Item of list is added to the set using add() function   
print('List items are: ',PeopleContact)      # It display list items
print('Set items are: ',PC)    # It display set items

 Output:

List items are:  [9993344556, 9630989900, 9993877389, 883456128, 9993877389, 7744536790]
Set items are:  {883456128, 9993344556, 9993877389, 9630989900, 7744536790}

  Code Explanation:

  • Comment line tells the goal of the program to programmers.
  • A list  named PeopleContact is created which contains the contact numbers of people.
  • A list is converted to set using the for loop and add() function of set.
  • A list and set items are displayed. Set items are unordered and unique.

Sample Problem-3: Using Approach-3

Consider a list named PeopleContact which contains Contact numbers of people. Create a python code to demonstrate the display of a unique element of a list using set comprehension method.

# Python code to convert a list to set using approach-3

PeopleContact=[9993344556,9630989900,9993877389,883456128,9993877389,7744536790 ]      # Here PeopleContact list is created
PC={i for i in PeopleContact}                     # Item of list is assigned to the set using list comprehension                     
print('List items are: ',PeopleContact)      # It display list items
print('Set items are: ',PC)    # It display set items

Output:

List items are:  [9993344556, 9630989900, 9993877389, 883456128, 9993877389, 7744536790]
Set items are:  {883456128, 9993344556, 9993877389, 9630989900, 7744536790}

 Code Explanation:

  • Comment line tells the goal of the program to programmers.
  • A list  named PeopleContact is created which contains the contact numbers of people.
  • A list is converted to a set using the set comprehension method.
  • A list and set items are displayed. Set items are unordered and unique.

Sample Problem-4: Using Approach-4

Consider a list named PeopleContact which contains Contact numbers of people. Create a python code to demonstrate the display of a unique element of a list  using dict.fromkeys() function.

# Python code to convert a list to set using approach-4
PeopleContact=[9993344556,9630989900,9993877389,883456128,9993877389,7744536790 ]      # Here PeopleContact list is created
PC=list(dict.fromkeys(PeopleContact)) # It convert dictionary to list
PCSet=set(PC)             # It convert PC list ot PCSet using set() function
print('List items are: ',PeopleContact)      # It display list items
print('Set items are: ',PCSet)    # It display set items

Output:

List items are:  [9993344556, 9630989900, 9993877389, 883456128, 9993877389, 7744536790]
Set items are:  {883456128, 9993344556, 9993877389, 9630989900, 7744536790}

Code Explanation:

  • Comment line tells the goal of the program to programmers.
  • A list  named PeopleContact is created which contains the contact numbers of people.
  • A list is created using dict.fromkeys() function. Then a list converted to a set using the set() function.
  • A list and set items are displayed. Set items are unordered and unique.

Conclusion

A list allows ordered and duplicate data items. Sometimes, we are required to access unique data items from the list, then we may convert a list to a set using various approaches such as set() function, add() function , set comprehension and dict.fromkeys() function. The conversion of list to set through the set comprehension is considered the best approach. Here, all approaches for the conversion of the list to set have been presented with python code, output and its explanation.