p

python tutorial - List Comprehension Python | Python List Comprehension and Slicing - learn python - python programming



List Comprehension

  • Python features functional programming tools like map and filter for mapping operations over sequences and collecting results.
  • Since this is such a common task in Python coding, Python made a new expression: the list comprehension which is more flexible than map and filter.
  • List comprehensions apply an arbitrary expression to items in an iterable rather than applying function.
  • It provides a compact way of mapping a list into another list by applying a function to each of the elements of the list.

List Comprehension vs. map

  • Python's built-in ord returns the ASCII integer code of a character:
>>> ord('A')
65
click below button to copy the code. By Python tutorial team
  • If we want to collect the ASCII codes of all characters in a string, the most straightforward method is using a for loop and append the results to a list:
>>> result = []
>>> for x in 'Dostoyevsky':
	result.append(ord(x))
	
>>> result
[68, 111, 115, 116, 111, 121, 101, 118, 115, 107, 121]
>>> 
click below button to copy the code. By Python tutorial team
  • If we use map, we can get the same result with a single function call:
>>> result = list(map(ord,'Dostoyevsky'))
>>> result
[68, 111, 115, 116, 111, 121, 101, 118, 115, 107, 121]
>>>
click below button to copy the code. By Python tutorial team
  • But, we can get the similar result from a list comprehension expression. While map maps a function over a sequence, list comprehensions map an expression over a sequence:
>>> result = [ord(x) for x in 'Dostoyevsky']
>>> result
[68, 111, 115, 116, 111, 121, 101, 118, 115, 107, 121]
>>> 
click below button to copy the code. By Python tutorial team
  • List comprehensions collect the result of applying an arbitrary expression to a sequence and return them in a new list.
  • The effect is similar to that of the for loop and the map call. List comprehensions become more convenient when we need to apply an arbitrary expression to a sequence:
>>> [x ** 3 for x in range(5)]
[0, 1, 8, 27, 64]
click below button to copy the code. By Python tutorial team
  • If we had to use map, we would need to write a function to implement the square operation, probably, lambda instead of using a def:
>>> list(map((lambda x: x ** 2),range(5)))
[0, 1, 4, 9, 16]
click below button to copy the code. By Python tutorial team
  • This does the job. It's only a little bit longer that the list comprehension. For more advance kinds of expressions, however, list comprehensions will often require considerably less typing.

List Comprehension with filter

  • If we use if with list compression, it is almost equivalent to the filter built-in.
  • Let's make examples using both schemes.
>>> 
>>> [x for x in range(10) if x % 2 == 0]
[0, 2, 4, 6, 8]

>>> list(filter((lambda x: x % 2 == 0), range(10)))
[0, 2, 4, 6, 8]

>>> result = []
>>> for x in range(10):
	if x % 2 == 0:
		result.append(x)
	
>>> result
[0, 2, 4, 6, 8]
>>> 
click below button to copy the code. By Python tutorial team
  • All of these use the modulus operator %, to extract even numbers. The filter call here is not much longer that the list comprehension either.
  • But we can combine an if and a map, in a single expression:
>>> [x ** 2 for x in range(10) if x % 2 == 0]
[0, 4, 16, 36, 64]
>>> 
click below button to copy the code. By Python tutorial team
  • We collect the squares of the even numbers from 0 to 9. The for loop skips numbers which the if on the right is false.
  • The expression on the left computes the squares.
  • The equivalent map requires a lot more work. We have to combine filter with map iteration:
>>> list( map((lambda x: x ** 2), filter((lambda x: x % 2== 0),range(10))) )
[0, 4, 16, 36, 64]
click below button to copy the code. By Python tutorial team
  • Actually, list comprehensions are more general.
  • We can code any number of nested for loop in a list comprehension, and each may have an optional associated if test.
  • When for loop are nested within a list comprehension, they work like equivalent for loop statement:
>>> result = []
>>> result = [ x ** y for x in [10, 20, 30] for y in [2, 3, 4]]
>>> result
[100, 1000, 10000, 400, 8000, 160000, 900, 27000, 810000]
>>> 
click below button to copy the code. By Python tutorial team
  • More verbose version is:
  • Though list comprehensions construct lists, they can iterate over any sequence:
>>> [x + y for x in 'ball' for y in 'boy']
['bb', 'bo', 'by', 'ab', 'ao', 'ay', 'lb', 'lo', 'ly', 'lb', 'lo', 'ly']
>>> 
click below button to copy the code. By Python tutorial team
  • Here is a much more complicated list comprehension example:
>>> [(x,y) for x in range(5) if x % 2 == 0 for y in range(5) if y % 2 == 1]
[(0, 1), (0, 3), (2, 1), (2, 3), (4, 1), (4, 3)]
click below button to copy the code. By Python tutorial team
  • The expression permutes even numbers from 0 through 4 with odd numbers from 0 through 4. Here is an equivalent version which is much more verbose:
>>> result = []
>>> for x in range(5):
	if x % 2 == 0:
		for y in range(5):
			if y % 2 == 1:
				result.append((x,y))

				
>>> result
[(0, 1), (0, 3), (2, 1), (2, 3), (4, 1), (4, 3)]
>>> 
click below button to copy the code. By Python tutorial team

List Comprehension with Matrixes

  • Let's see how the list comprehension works with Matrixes. For example, we have two 3 x 3 matrixes as lists of nested list:
>>> M1 = [[1, 2, 3],
          [4, 5, 6],
          [7, 8, 9]]
>>> M2 = [[9, 8, 7],
          [6, 5, 4],
          [3, 2, 1]]
click below button to copy the code. By Python tutorial team
  • We can index rows and columns within rows:
>>> M1[2]
[7, 8, 9]
>>> M1[2][2]
9
click below button to copy the code. By Python tutorial team
  • List comprehensions are powerful tools for processing such structures because they automatically scan rows and columns for us.
  • For example, though this structure stores the matrix by rows, to collect the second column we can simply iterate across the rows and pull out the desired column.
  • We can also iterate through positions in the rows and index as we go:
>>> [r[2] for r in M1]
[3, 6, 9]
click below button to copy the code. By Python tutorial team
  • Here, we pulled out column 3 from M1. We can get the same result from the following list comprehension.
>>> [M1[r][2] for r in (0, 1, 2)]
[3, 6, 9]
click below button to copy the code. By Python tutorial team

When to Use List Comprehension

  • We typically should use simple for loops when getting started with Python, and map.
  • Use comprehension where they are easy to apply. However, there is a substantial performance advantage to use list comprehension.
  • The map calls are roughly twice as fast as equivalent for loops. List comprehensions are usually slightly faster than map calls. This speed difference is largely due to the fact that map and list comprehensions run at C language speed inside the interpreter. It is much faster that stepping through Python for loop code within the Python Virtual Machine (PVM).
  • However, for loops make logic more explicit, we may want to use them on the grounds of simplicity. On the other hand, map and list comprehensions are worth knowing and using for simpler kinds of iterations if the speed of application is an important factor.

List comprehension is an elegant way to define and create list in python. We can create lists just like mathematical statements and in one line only. The syntax of list comprehension is easier to grasp.

python - Sample - python code :

A list comprehension generally consist 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.
click below button to copy the code. By Python tutorial team
# Python program to demonstrate list comprehension in Python 
 
# below list contains square of all odd numbers from
# range 1 to 10
odd_square = [x ** 2 for x in range(1, 11) if x % 2 == 1]
print odd_square
 
# for understanding, above generation is same as,
odd_square = []
for x in range(1, 11):
    if x % 2 == 1:
        odd_square.append(x**2)
print odd_square
 
# below list contains power of 2 from 1 to 8
power_of_2 = [2 ** x for x in range(1, 9)]
print power_of_2
 
# below list contains prime and non-prime in range 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
 
# list for lowering the characters
print [x.lower() for x in ["A","B","C"]]
 
# list which extracts number
string = "my phone number is : 11122 !!"
 
print("\nExtracted digits")
numbers = [x for x in string if x.isdigit()]
print numbers
 
# A list of list for multiplication table
a = 5
table = [[a, b, a * b] for b in range(1, 11)]
 
print("\nMultiplication Table")
for i in table:
    print i
click below button to copy the code. By Python tutorial team

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]



After getting the list, we can get a part of it using python’s slicing operator which has following syntax :

python - Sample - python code :

[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
click below button to copy the code. By Python tutorial team

So [: stop] will slice list from starting till stop index and [start : ] will slice list from start index till end Negative value of steps shows right to left traversal instead of left to right traversal that is why [: : -1] prints list in reverse order.

# Let us first create a list to demonstrate slicing
# lst contains all number from 1 to 10
lst = range(1, 11)
print lst
 
#  below list has numbers from 2 to 5
lst1_5 = lst[1 : 5]
print lst1_5
 
#  below list has numbers from 6 to 8
lst5_8 = lst[5 : 8]
print lst5_8
 
#  below list has numbers from 2 to 10
lst1_ = lst[1 : ]
print lst1_
 
#  below list has numbers from 1 to 5
lst_5 = lst[: 5]
print lst_5
 
#  below list has numbers from 2 to 8 in step 2
lst1_8_2 = lst[1 : 8 : 2]
print lst1_8_2
 
#  below list has numbers from 10 to 1
lst_rev = lst[ : : -1]
print lst_rev
 
#  below list has numbers from 10 to 6 in step 2
lst_rev_9_5_2 = lst[9 : 4 : -2]
print lst_rev_9_5_2
click below button to copy the code. By Python tutorial team

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 filter function to filter a list based on some condition provided as a lambda expression as first argument and list as second argument, example of which is shown below :

python - Sample - python code :

#  filtering odd numbers
lst = filter(lambda x : x % 2 == 1, range(1, 20))
print lst
 
#  filtering odd square which are divisble 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 < 0), range(-5,5))
print lst
 
#  implementing max() function, using
print reduce(lambda a,b: a if (a > b) else b, [7, 12, 45, 100, 15])
click below button to copy the code. By Python tutorial team

Output:

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

Wikitechy tutorial site provides you all the learn python , python cursus online , python programming course free , where to learn python programming , free online python course , online free python course , learn python coding online , python developers , python certification free , python learning course , online course python

Related Searches to Python List Comprehension and Slicing