Use of comprehension in python

Use of comprehension in python

Instructor-svgAl-Mamun Sarkar
Mar 25 , 2020

Use of comprehension in the python programming language. 

my_list = [i**2 for i in range(20) if i%2 == 0]
print(my_list)

a_list = ['Mamun', 'Khan', 'Sarkar', 'a', 'b', 'c']
n_list = [i for i in a_list if len(i) > 1]
print(n_list)

a_list = ['name', 'country', 'career']
b_list = ['Mamun', 'Bangladesh', 'TaleTalk']
my_dec = {i: j for i, j in zip(a_list, b_list)}
print(my_dec)

 

Output:

[0, 4, 16, 36, 64, 100, 144, 196, 256, 324]
['Mamun', 'Khan', 'Sarkar']
{'name': 'Mamun', 'country': 'Bangladesh', 'career': 'TaleTalk'}

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram