# Understanding Python List and Slicing

Python Methods and Functions

Understanding the list — it is an elegant way to define and create a list in Python. We can create lists in the same way as mathematical expressions and only in one line. The list comprehension syntax is easier to understand.

` A list comprehension generally consists of these parts: Output expression, input sequence, a variable representing member of input sequence and an optional predicate part. For example: lst = [x ** 2 for x in range (1, 11) if x% 2 == 1] here, x ** 2 is output expression, range (1, 11) is input sequence, x is variable and if x% 2 == 1 is predicate part. `

 ` # Python program to demonstrate list comprehension in Python `   ` # The list below contains the square of all odd numbers from ` ` # range from 1 to 10 ` ` odd_square ` ` = ` ` [x ` ` * ` ` * ` ` 2 ` ` for ` ` x ` ` in ` ` range ` ` (` ` 1 ` `, ` ` 11 ` `) ` ` if ` ` x ` `% ` ` 2 ` ` = ` ` = ` ` 1 ` `] ` ` print ` ` odd_square `   ` # for understanding, the above generation is the same as ` ` odd_square ` ` = ` ` [] ` ` for ` ` x ` ` in ` ` range ` ` (` ` 1 ` `, ` ` 11 ` `): ` ` if ` ` x ` `% ` ` 2 ` ` = ` ` = ` ` 1 ` `: ` ` odd_square.append (x ` ` * ` ` * ` ` 2 ` `) ` ` print ` < code class = "plain"> odd_square   ` # The list below contains degree 2 1 to 8 ` ` power_of_2 ` ` = ` ` [ ` ` 2 ` ` * ` ` * ` ` x ` ` for ` ` x ` ` in ` ` range ` ` (` ` 1 ` `, ` ` 9 ` `)] ` ` print ` ` power_of_2 ` ` `  ` # the list below contains simple and non-simple ones ranging from 1 to 50 ` ` noprimes ` ` = ` ` [j ` for ` i ` ` in ` ` range ` ` (` ` 2 ` `, ` ` 8 ` `) ` ` for ` ` j ` ` in ` ` range ` ` (i ` ` * ` ` 2 ` `, ` ` 50 ` `, i)] ` ` primes ` ` = ` ` [x ` ` for ` ` x ` ` in ` ` range ` ` (` ` 2 ` `, ` ` 50 ` `) ` ` if ` ` x ` ` not ` ` in ` ` noprimes] ` ` print ` ` primes `   ` # omission list ` ` print ` ` [ x.lower () ` ` for ` ` x ` ` in ` ` [` ` "A" ` `, ` ` "B" ` `, ` ` "C" ` `]] `   ` # list that extracts the number ` ` string ` ` = ` `" my phone number is: 11122 !! "` `                        `  ` print ` ` (` ` "Extracted digits" ` `) ` ` numbers ` ` = ` ` [x ` ` for ` ` x ` ` in ` ` string ` ` if ` ` x.isdigit ()] ` ` print ` ` numbers `   ` # List list for multiplication table ` ` a ` ` = ` ` 5 ` ` table ` ` = ` ` [[a, b, a ` ` * ` ` b] ` ` for ` ` b ` ` in ` ` range ` ` (` ` 1 ` `, ` ` 11 ` `)] `   ` print ` ` (` `" Multiplication Table "` `) ` ` for ` ` i ` ` in ` ` table: ` ` print ` ` i `

Output:

` [1, 9, 25, 49, 81] [1, 9, 25, 49, 81] [2, 4, 8, 16, 32 , 64, 128, 256] [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] ['a',' b', 'c' ] Extracted digits ['1',' 1', '1',' 2', '2'] Multiplication Table [5, 1, 5] [5, 2, 10] [5, 3, 15] [5, 4, 20] [5, 5, 25] [5, 6, 30] [5, 7, 35] [5, 8, 40] [5, 9, 45] [5, 10, 50] `

Once we get the list, we can get a part of it using the slicing operator in python, which has the following syntax:

`  [start : stop: steps]  which means that slicing will start from index start will go up to  stop  in  step  of steps. Default value of start is 0, stop is last index of list and for step it is 1 `

So [: stop] will slice the list from start to end of index, and [start:] will slice the list from the beginning of the index to the end. Negative steps show right-to-left traversal instead of left-to-right traversal, so [:: -1] prints the list in reverse order.

 ` # Let's create a list first to demonstrate slicing ` ` # lst contains all numbers from 1 to 10 ` ` lst ` ` = ` ` range ` ` (` ` 1 ` `, ` ` 11 ` `) ` ` print ` ` lst `   ` # below the list has numbers from 2 to 5 ` ` lst1_5 ` ` = ` ` lst [` ` 1 ` `: ` ` 5 ` `] ` ` print ` ` lst1_5 `   ` # below the list has numbers from 6 to 8 ` ` lst5_8 ` ` = ` ` lst [` ` 5 ` `: ` ` 8 ` `] ` ` print ` ` lst5_8 ` ` `  ` # the list below contains numbers from 2 to 10 ` ` lst1_ ` ` = ` ` lst [` ` 1 ` `:] ` ` print ` ` lst1_ ` `   # below the list has numbers from 1 to 5 `` lst_5 = lst [: 5 ] print lst_5   # in the list below the numbers from 2 to 8 in step 2 lst1_8_2 = lst [ 1 : 8 : 2 ] print lst1_8_2   # below the list has numbers from 10 to 1 lst_rev = lst [:: - 1 ] print lst_rev    # in the list below the numbers from 10 to 6 in step 2 lst_rev_9_5_2 = lst [ 9 : 4 : - 2 ] print lst_rev_9_5_2 `

Output:

` [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [2, 3, 4, 5] [6, 7, 8] [2, 3, 4, 5, 6, 7, 8, 9 , 10] [1, 2, 3, 4, 5] [2, 4, 6, 8] [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] [10, 8, 6 ] `

We can use the filter function to filter a list based on some condition, represented as a lambda expression in as the first argument, and a list as the second argument, an example of which is shown below:

 ` # filtering odd numbers ` ` lst ` ` = ` ` filter ` ` (` ` lambda ` ` x: x ` `% ` ` 2 ` ` = ` ` = ` ` 1 ` `, ` ` range ` ` ( ` ` 1 ` `, ` ` 20 ` `)) ` ` print ` ` lst ``   # filtering an odd square that is divisible by 5 lst = filter ( lambda x: x % 5 = = 0 ,  [x * * 2 for x in range ( 1 , 11 ) if x % 2 = = 1 ]) print lst   # filtering negative numbers lst = filter (( lambda x: x & lt;  0 ), range ( - 5 , 5 )) print lst   # implementing the max () function using print reduce ( lambda a, b: a if (a & gt ; b) else b, [ 7 , 12 , 45 , 100 , 15 ]) `

Output:

` [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] [ 25] [-5, -4, -3, -2, -1] 100 `