The set is a data container which contains only unique data elements. It is also referred to as collection of heterogeneous data elements. Set is unordered and immutable. Immutable means, we can not change or delete an item of the set. Set also does not support indexing unlike list and tuple. A set is created through set variable name and curly braces i.e. setname={}. A syntax and example to create a set is given below.
Syntax:
setname = [element1, element2, element3,…..elementN]
Example:
StudentMarks={65,50,75,80,50,40,70,75,85,35,90,65}
Employee=[‘Jhon’, ‘Male’,‘NewYork’, 37, 55000 ]
An above example shows creating a set named StudentMarks which contains marks of students. A python code to create a set is demonstrated below.
Python Code to create a Set:
# Python code to create set
StudentMarks={65,50,75,80,50,40,70,75,85,35,90,65} # It create set name StudentMarks
print('Set elements are: ',StudentMarks) # It display unique and random order elements of StudentMarks
Output:
Set elements are: {65, 35, 70, 40, 75, 80, 50, 85, 90}
Code Explanation:
- Comment line tells the goal of the program to programmers.
- A set named StudentMarks is created which contains student marks.
- Elements of the set are displayed.
Why Is There A Need To Convert Set to List In Python
There are several cases where we need to change the set to list in Python during our program. Few of the use cases are listed below:
- To change or remove set elements: A set is immutable or unchangeable that means it does not allow to change or remove an element. Suppose we need to change or remove set elements then it is required to convert set to list. A list is mutable, which means list elements may be changed or removed. Therefore, a set is converted to a list.
- To preserve order of set elements: As we know a set is unordered. It means the set does not have any kind of indexing where elements have random order. If we want to preserve and access set elements in an ordered manner then it is required to convert into a list.
- To store duplicate elements: Set does not allow duplicate elements. If we require adding duplicate elements then we should have a list instead of a set.
Approaches: How to Convert Set to List
To convert a set to a list, various approaches are available. There are some approaches listed below.
- Approach-1: Using list() function
- Approach-2: Using sorted() function
- Approach-3: Using list comprehension
- Approach-4: Using Unpack set inside parentheses i.e. [*s,] method
- Approach-5: Using map() function
- Approach-6: Using for loop
Approach-1: Using list() Function
With the help of a python inbuilt function named list() function, we can convert a set to a list. List() function takes set name as argument and returns list elements. A python code to convert a set to a list using list() function demonstrated below.
# Python code to convert set to list using list() function
StudentMarks={65,50,75,80,50,40,70,75,85,35,90,65} # It create set name StudentMarks
print('Set elements are: ',StudentMarks) # It display unique and random order elements of StudentMarks
SM=list(StudentMarks) # It convert set StudentMarks to list SM using list() function
print('List Elements are: ',SM) # It display list elements
Output:
Set elements are: {65, 35, 70, 40, 75, 80, 50, 85, 90}
List Elements are: [65, 35, 70, 40, 75, 80, 50, 85, 90]
Code Explanation:
- Comment line tells the goal of the program to programmers.
- A set named StudentMarks is created which contains the student marks.
- A set is converted to list using list() function
- A set and list items are displayed. List items are ordered.
Approach-2: Using a sorted() function
Sorted() function is another approach to convert a set to list. Sorted() function converts a set to list into a definite order. A python code to convert a set to list using sorted() function demonstrated below.
# Python code to convert set to list using sorted() function
StudentMarks={65,50,75,80,50,40,70,75,85,35,90,65} # It create set name StudentMarks
print('Set elements are: ',StudentMarks) # It display unique and random order elements of StudentMarks
SM=sorted(StudentMarks) # It convert set StudentMarks to list SM using sorted() function
print('List Elements are: ',SM) # It display list elements
Output:
Set elements are: {65, 35, 70, 40, 75, 80, 50, 85, 90}
List Elements are: [35, 40, 50, 65, 70, 75, 80, 85, 90]
Code Explanation:
- Comment line tells the goal of the program to programmers.
- A set named StudentMarks is created which contains the student marks.
- A set is converted to list using sorted() function
- A set and list items are displayed. List items are ordered.
Approach-3: Using List Comprehension
With the help of the list comprehension approach, a set is converted to a list. List comprehension allows you to create a list using a for loop in single line code. It is a short way to create a list. A python code to convert a set to list using list comprehension demonstrated below.
# Python code to convert set to list using list comprehension
StudentMarks={65,50,75,80,50,40,70,75,85,35,90,65} # It create set name StudentMarks
print('Set elements are: ',StudentMarks) # It display unique and random order elements of StudentMarks
SM=[s for s in StudentMarks] # It convert set StudentMarks to list SM using sorted() function
print('List Elements are: ',SM) # It display list elements
Output:
Set elements are: {65, 35, 70, 40, 75, 80, 50, 85, 90}
List Elements are: [65, 35, 70, 40, 75, 80, 50, 85, 90]
Code Explanation:
- Comment line tells the goal of the program to programmers.
- A set named StudentMarks is created which contains the marks of students.
- Each set item is accessed and added to the list using list comprehension.
- A list and set items are displayed. List items are ordered.
Approach-4: Using map() function
Map() function of python is another approach to convert a set to a list. It used lambda function and return list as a result. A python code to convert a set to list using map() function demonstrated below.
# Python code to convert set to list using map() function
StudentMarks={65,50,75,80,50,40,70,75,85,35,90,65} # It create set name StudentMarks
print('Set elements are: ',StudentMarks) # It display unique and random order elements of StudentMarks
SM=list(map(lambda i: i, StudentMarks)) # It convert set to list using lambda and map() function
print('List Elements are: ',SM) # It display list elements
print('Set items are: ',Emp) # It display set items
Output:
Set elements are: {65, 35, 70, 40, 75, 80, 50, 85, 90}
List Elements are: [65, 35, 70, 40, 75, 80, 50, 85, 90]
Code Explanation:
- Comment line tells the goal of the program to programmers.
- A set named StudentMarks is created which contains the marks of students.
- A set is converted to a list using lambda and map() functions.
- A list and set items are displayed. List items are ordered.
Approach-5: Using for loop
With the help of a for loop, we can convert a set to list. This method converts set elements to list elements step by step in number of iterations. A python code to convert a set to a list using a for loop demonstrated below.
# Python code to convert set to list using for loop
StudentMarks={65,50,75,80,50,40,70,75,85,35,90,65} # It create set name StudentMarks
print('Set elements are: ',StudentMarks) # It display unique and random order elements of StudentMarks
SM=[]
for i in StudentMarks:
SM.append(i) # It convert each element of set to list using for loop
print('List Elements are: ',SM) # It display list elements
Output:
Set elements are: {65, 35, 70, 40, 75, 80, 50, 85, 90}
List Elements are: [65, 35, 70, 40, 75, 80, 50, 85, 90]
Code Explanation:
- Comment line tells the goal of the program to programmers.
- A set named StudentMarks is created which contains the marks of students.
- A set is converted to a list using the for loop and append() function of list.
- A list and set items are displayed. List items are ordered.
Best Approach- List Comprehension
List comprehension method is considered as best method to convert sets to lists for the following benefits
- The list Comprehension approach provides a short and very convenient way to convert a set to list.
- It does not use any in-built function such as list() and sorted() functions.
- List Comprehension leads short length of code
- Its time complexity is also less compared to other approaches.
Sample problem related to convert set to a list in Python
Sample Problem-1: Using Approach-1
Problem Definition: Create a python code to demonstrate how to convert a set to list using list() function. For this, create a set named EmployessPost which contains the post name of the employee. Then convert the created set to a list using list() function.
Solution (Python Code):
# Python code to convert set to list using list() function
EmployeePost={'Manager','Supervisor','Developer','Administrator','HR'} # It create set name EmployeePost
print('Set elements are: ',EmployeePost) # It display unique and random order elements of EmployeePost
EP=list(EmployeePost) # It convert set EmployeePost to list EP using list() function
print('List Elements are: ',EP) # It display list elements
Output:
Set elements are: {'Administrator', 'Manager', 'HR', 'Supervisor', 'Developer'}
List Elements are: ['Administrator', 'Manager', 'HR', 'Supervisor', 'Developer']
Code Explanation:
- Comment line tells the goal of the program to programmers.
- A set named EmployeePost is created which contains posts of employees.
- A set is converted to list using list() function
- A set and list items are displayed. List items are ordered.
Sample Problem-2:Using Approach-2
Consider a set named EmployessPost which contains the post name of the employee. Create a python code to demonstrate display elements of set in sorted (ascending) order using the sorted() function.
# Python code to convert set to list using sorted() function
EmployeePost={'Manager','Supervisor','Developer','Administrator','HR'} # It create set name EmployeePost
print('Set elements are: ',EmployeePost) # It display unique and random order elements of EmployeePost
EP=sorted(EmployeePost) # It convert set EmployeePost to list EP using sorted() function
print('List Elements are: ',EP) # It display list elements
Output:
Set elements are: {'Administrator', 'Manager', 'HR', 'Supervisor', 'Developer'}
List Elements are: ['Administrator', 'Developer', 'HR', 'Manager', 'Supervisor']
Code Explanation:
- Comment line tells the goal of the program to programmers.
- A set named EmployeePost is created which contains posts of employees.
- A set is converted to list using sorted() function
- A set and list items are displayed. List items are ordered.
Sample Problem-3: Using Approach-3
Consider a set named EmployessPost which contains the post name of the employee. Create a python code to demonstrate display elements of a set in sorted (ascending) order using list comprehension method.
# Python code to convert set to list using list comprehension
EmployeePost={'Manager','Supervisor','Developer','Administrator','HR'} # It create set name EmployeePost
print('Set elements are: ',EmployeePost) # It display unique and random order elements of EmployeePost
EP=[e for e in EmployeePost] # It convert set EmployeePost to list EP using list comprehension
print('List Elements are: ',EP) # It display list elements
Output:
Set elements are: {'Administrator', 'Manager', 'HR', 'Supervisor', 'Developer'}
List Elements are: ['Administrator', 'Manager', 'HR', 'Supervisor', 'Developer']
Code Explanation:
- Comment line tells the goal of the program to programmers.
- A set named EmployeePost is created which contains posts of employees.
- A set is converted to list using list comprehension
- A set and list items are displayed. List items are ordered.
Sample Problem-4: Using Approach-4
Consider a set named EmployessPost which contains the post name of the employee. Create a python code to demonstrate display elements of a set random order using map() function.
# Python code to convert set to list using map() function
EmployeePost={'Manager','Supervisor','Developer','Administrator','HR'} # It create set name EmployeePost
print('Set elements are: ',EmployeePost) # It display unique and random order elements of EmployeePost
EP=list(map(lambda i: i, EmployeePost)) # It convert set to list using lambda and map() function
print('List Elements are: ',EP) # It display list elements
Output:
Set elements are: {'Administrator', 'Manager', 'HR', 'Supervisor', 'Developer'}
List Elements are: ['Administrator', 'Manager', 'HR', 'Supervisor', 'Developer']
Code Explanation:
- Comment line tells the goal of the program to programmers.
- A set named EmployeePost is created which contains posts of employees.
- A set is converted to list using map() function
- A set and list items are displayed. List items are ordered.
Sample Problem-5: Using Approach-5
Consider a set named EmployessPost which contains the post name of the employee. Create a python code to demonstrate display elements of a set random order using a for loop.
# Python code to convert set to list using for loop
EmployeePost={'Manager','Supervisor','Developer','Administrator','HR'} # It create set name EmployeePost
print('Set elements are: ',EmployeePost) # It display unique and random order elements of EmployeePost
for i in EmployeePost:
EP.append(i) # It convert each element of set to list using for loop
print('List Elements are: ',EP) # It display list elements
Output:
Set elements are: {'Administrator', 'Manager', 'HR', 'Supervisor', 'Developer'}
List Elements are: ['Administrator', 'Manager', 'HR', 'Supervisor', 'Developer']
Code Explanation:
- Comment line tells the goal of the program to programmers.
- A set named EmployeePost is created which contains posts of employees.
- A set is converted to list using for loop
- A set and list items are displayed. List items are ordered.
Conclusion
A set does not allow to change and remove elements. To change and delete, set elements then it requires to convert a set to a list. A set is converted into a list using various approaches such as list() function, sorted() function , list comprehension and map() function. The conversion of set to list through the list comprehension is considered as the best approach.
Here, all approaches for the conversion of the set to list have been presented with python code, output and its explanation.