Adding unpacking list elements to variables (#19437)

* Adding unpacking list elements to variables

* fix: indentations
This commit is contained in:
Brian R
2018-10-16 03:18:33 -05:00
committed by Aditya
parent 0a1f237a23
commit 891152863a

View File

@@ -69,6 +69,28 @@ _Unpacking lists for python-3_
>>> print(*my_list)
1 2 9 16 25
```
_unpacking elements from the list to variables python-3_
We can unpack the elements of the list, and assign to variables.
```
>>> my_list = [2, 4, 6, 8]
>>> a, b, c, d = my_list
>>>print(a, b, c, d)
2 4 6 8
```
Same way, if we add * before the var name, this will automatically take the left elements of the list
```
>>> my_list = [2, 4, 6, 8, 10]
>>>a, *b = my_list
>>>print(a, b)
2 [4, 6, 8, 10]
```
Also, we can do this:
```
>>> my_list = [2, 4, 6, 8, 10]
>>>a, *b, c = my_list
>>>print(a, b, c)
2 [4, 6, 8] 10
```
**Reverse a `list`:**
```shell
>>> my_list = [1, 2, 9, 16, 25]