From 891152863ace6305878f580701703f9d6125c6c0 Mon Sep 17 00:00:00 2001 From: Brian R Date: Tue, 16 Oct 2018 03:18:33 -0500 Subject: [PATCH] Adding unpacking list elements to variables (#19437) * Adding unpacking list elements to variables * fix: indentations --- .../pages/guide/english/python/lists/index.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/client/src/pages/guide/english/python/lists/index.md b/client/src/pages/guide/english/python/lists/index.md index 0e56109b79..dfd77df2e2 100644 --- a/client/src/pages/guide/english/python/lists/index.md +++ b/client/src/pages/guide/english/python/lists/index.md @@ -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]