How to convert set to list in python

(*-*)浩
Release: 2019-11-29 10:25:39
Original
41599 people have browsed it

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

How to convert set to list in 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'}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template