# Python List Comprehension Examples

1 You are given a sentence that has been input by a user. Using list comprehension, create a list called newList, which contains each word in lowercase and its length for words longer than 5 characters.

Input

`Existence =" Everyone WAS coping"`

Output

`newList :[('everyone',8), ('coping',6)]`

Here, you have to accept words whose length is greater than 5, so you have to use if condition to filter words. After that, using the len() method, you will get the length of the words.

```Existence = "Everyone WAS coping"
newlist = [(e, len(e)) for e in Existence.split() if len(e)>5]
print(newlist)```

2 Using a single list comprehension, print all the numbers in the range 1-100 that are either divisible by 10 or 11.

`[i for i in range(1, 101) if i%10==0 or i%11==0]`

Output

`[10, 11, 20, 22, 30, 33, 40, 44, 50, 55, 60, 66, 70, 77, 80, 88, 90, 99, 100]`

3 Using list comprehensions, create a list of all even integers between 0 and 1000, and then sum the list using a for-loop. You must print the sum.

```evenNumbers = [i for i in range(0, 101) if i%2==0]
sum = 0
for even in evenNumbers:
sum = sum + even
print('Sum is', sum)```

Output

`Sum is 2550`

4 Write a python code to multiply each number in a given list by 10 using list comprehension.

Input

`a = [1, 2, 3, 4, 5]`

Code

```a = [1,2,3,4,5]
[number*10 for number in a]```

5 Write the function square_odds that takes a list of integers `nums` and returns the squares of the odd numbers in the list `nums`. You must use list comprehension.

```def square_odds(num):
return [number*number for number in num if number%2!=0]
print(square_odds([]))
print(square_odds([1,2,3,4,5]))
print(square_odds([7,3,5]))
print(square_odds([2,4,8]))```

Output

```[]
[1, 9, 25]
[49, 9, 25]
[]```

6 A sentence input by the user is given. Using list comprehension, create a list of the words that do not begin with a vowel.

```sentence = "This is a long list of words"
[word for word in sentence.split() if word!='a' and word!='e' and word!='i' and word!='o' and word!='u']```

7 This program is about list comprehension.

```list1 = [2, 5, 7, 8]
list2 = [1, 2]```

(a) Use list comprehension with list1 as input sequence to generate this list:

`[5, 11, 15, 17]`

Display the list.

Code

`[item*2+1 for item in list1]`

(b) Use list comprehension with list1 as input sequence to generate this list:

`[3, 9]`

Display the list.

Code

`[item + 1 for item in list1 if item==2 or item==8]`

(c) Use list comprehension with list1 as input sequence to generate this list:

`[[2, 3], [5, 6], [7, 8], [8, 9]]`

Display the list.

Code

`[[item, item + 1] for item in list1]`

(d) Use list comprehension with list1 and list2 as input sequences to generate this list:

`[[2, 1], [2, 2], [5, 1], [5, 2], [7, 1], [7, 2], [8, 1], [8, 2]]`

Display the list.

Code

`[[item, i] for item in list1 for i in list2 ]`

(e) Use nested list comprehension with list1 and list2 as input sequences to generate this list:

`[[3, 4], [6, 7], [8, 9], [9, 10]]`

Display the list.

Code

`[[item+i for i in list2] for item in list1 ]`

8 Demonstrate how to use Python's list comprehension syntax to produce the list [1, 10, 100, 1000, 10000, 100000].

`[10**i for i in range(0, 6)]`

9 Demonstrate how to use Python's list comprehension syntax to produce the list [0, 2, 6, 12, 20, 30, 42, 56, 72, 90].

`[count*value for count, value in enumerate(range(1, 11))]`

10 Demonstrate how to use Python's list comprehension syntax to produce the list ['a', 'b', 'c', ... , 'z'], but without having to type all 26 such characters literally.

`[chr(a) for a in range(97,123)]`

11 How do you write a list comprehension to print out the cubes of all the numbers from 1^3 to 10^3 in one line of code?

`[i**3 for i in range(1,11)]`

12 How do you write a list comprehension to print out all of the values (2x^2 - 1) for the even numbers [1, 20]?

`[2*i**2 - 1 for i in range(1, 21) if i%2==0]`

13 How do you write a list comprehension of the tuples of (x, x^2) for x in list [5, 14, 15, 16, 20, 23]?

```lst = [5, 14, 15, 16, 20, 23]
[(item, item**2) for item in lst]```