Today I will share with you an example of conversion between python-list and set, which has a good reference value.
Recommended manual : Basic introductory tutorial on Python
In fact, in python, converting set to list is very simple. Just put the value of set directly into the brackets of list(), and vice versa. , the same goes for converting list to set. (Recommended learning: Python video tutorial)
>>> s=set('123456') >>> print(s) {'6', '4', '5', '1', '2', '3'} >>> l=list(s) >>> l.sort() >>> print(l) ['1', '2', '3', '4', '5', '6'] >>> m=set(l) >>> print(m) {'6', '4', '5', '1', '2', '3'}
It can be seen that set and lsit can be freely converted. When deleting multiple/massive duplicate elements in the list, you can first convert to set and then convert Go back to the list and sort (set is not sorted). This method is not only convenient but also efficient.
Recommended related articles:
1.The difference between list and set in Python
Recommended related videos:
1.Little Turtle’s zero-based entry-level Python video tutorial
For more Python-related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to convert set to list in python. For more information, please follow other related articles on the PHP Chinese website!