- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert set into a list in Python
As part of data analysis using Python we may be required to to convert the data container from set to list. In this article we'll see e how to solve this requirement.
With list
This is a straightforward approach in which we directly apply the list function on the given set. The elements get converted into elements of a list.
Example
setA = {'Mon', 'day', '7pm'} # Given Set print("Given set : \n", setA) res = (list(setA) ) # Result print("Final list: \n",res)
Output
Running the above code gives us the following result −
Given set : ['7pm', 'Mon', 'day'] Final list: ['7pm', 'Mon', 'day']
With *
The star operator can take in the list and by applying the square brackets around it we get a list.
Example
setA = {'Mon', 'day', '7pm'} # Given Set print("Given set : \n", setA) res = [*setA, ] # Result print("Final list: \n",res)
Output
Running the above code gives us the following result −
Given set : {'Mon', '7pm', 'day'} Final list: ['Mon', '7pm', 'day']
Advertisements
To Continue Learning Please Login
Login with Google