Further explained how to initialize sets (#29486)
This commit is contained in:
committed by
Christopher McCormack
parent
f8675b0bda
commit
2462cfa1d9
@ -1,7 +1,7 @@
|
|||||||
---
|
---
|
||||||
title: Learn About Python Sets
|
title: Learn About Python Sets
|
||||||
---
|
---
|
||||||
`Set`s in Python are a type of mutable but unordered data structure, which can only contain *unique* elements.
|
`Set`s in Python are a type of mutable but unordered data structure, which can only contain *unique* elements. In other words, it is equivalent to sets in math.
|
||||||
|
|
||||||
**Creation:**
|
**Creation:**
|
||||||
|
|
||||||
@ -31,9 +31,15 @@ However, if elements are included within the curly brackets, then it would be ac
|
|||||||
<class 'set'>
|
<class 'set'>
|
||||||
````
|
````
|
||||||
|
|
||||||
### Converting List to Set
|
## Converting Iterable to Set
|
||||||
|
If `set(...)` contains an iterable such as a list, a string, or a tuple as an element, it will return a set containing its' elements. This will remove all duplicate values from the list.
|
||||||
|
```python
|
||||||
|
>>> example_set_3 = set('some string')
|
||||||
|
>>> example_set_3
|
||||||
|
{' ', 't', 'g', 'o', 'r', 'i', 's', 'e', 'n', 'm'}
|
||||||
|
```
|
||||||
|
|
||||||
If you want to convert a list to a set, you can do that by using the `set()` function. This will remove all duplicate values from the list.
|
If you want to convert an iterable like a list to a set, you can do that by passing it to the `set()` function.
|
||||||
```python
|
```python
|
||||||
>>> a = [11,2,2,6,6,4,8,9,9,7]
|
>>> a = [11,2,2,6,6,4,8,9,9,7]
|
||||||
>>> a = set(a)
|
>>> a = set(a)
|
||||||
|
Reference in New Issue
Block a user