0% found this document useful (0 votes)
32 views4 pages

Question On List Comprehension

Uploaded by

khan2547abdul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

Question On List Comprehension

Uploaded by

khan2547abdul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1 - Add 1 to 10 numbers to a list

Expected output : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

2 - Get the first element from each nested list in a list | li = [[1,2,3],[4,5,6],
[7,8,9],[10,11,12],[13,14,15]]

Expected output : [1, 4, 7, 10, 13]

Use list comprehension to extract the first element of each nested list

3 - Find the intersection(same element) of 2 lists | li1 = [1,2,3] ,li2 = [2,3,4]

Expected output : [2, 3]

4 - Flatten a list of lists with a list comprehensions | li = [[1,2,3],[4,5,6]]

Expected output : will be [1, 2, 3, 4, 5, 6]

5 - Python program to count Even and Odd numbers in a List.

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

Expected output :

Number of even numbers in the list: 5


Number of odd numbers in the list: 5

6 - Python program to count Even and Odd numbers in a List.

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

Expected output :

['number is odd : 1', 'number is even : 2', 'number is odd : 3', 'number is even :
4', 'number is odd : 5', 'number is even : 6', 'number is odd : 7', 'number is even
: 8', 'number is odd : 9', 'number is even : 10']

7 - Given a list of numbers, remove floats (numbers with decimals)

original_list = [2,3.84,0.04,8,9]

Expected output : [2, 8, 9]

8 - Given a list of numbers, remove integer

original_list = [2,3.84,0.04,8,9]

Expected output : [3.84, 0.04]

9 - "Write a Python program to filter out strings from a list 'l' that contain only
alphabetic characters and don't start with a special character or a number.

l=["good", "oh!", "excellent!", "#450"]

Expected output : ['good']


10 - Multiply every element in a list by 5 | a = [10,20,30,40,50]

11 - Given a list of numbers, remove all odd numbers from the list:

numbers = [3,5,45,97,32,22,10,19,39,43]

Expected output : [32, 22, 10]

12 - v = [2,3,4]
s = -3

Expected output : [-6,-9,-12 ]

13 - Add sqaures l = [1,2,3,4,5,6]

Expected output : [1,4,9,16,25,36]

14 - print all the numbers divisible by 5 in the range of 1 to 50

Expected output : [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

15 - find the languages which start with letter p

languages = ['java','python','php','c','javascript']

Expected output : ['python', 'php']

16 - find the fruits which start with letter "a"

basket = ['apple','guava','cherry','banana']
my_fruits = ['apple','kiwi','grapes','banana']

Expected output : ['apple']

17 - write a program to print into a list

a = "hello world"

Expected output : ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

18 - Filter even values out of a list

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

Expected output : [2, 4, 6, 8, 10]

19 - Remove empty strings from the list of strings

list1 = ["Mike", "", "Emma", "sam", "", "jhon"]

Expected output : ['Mike', 'Emma', 'sam', 'jhon']

20 - Remove all occurrences of a specific item from a list.remove all occurrences


of item 20

list1 = [5, 20, 15, 20, 25, 50, 20]

[5, 15, 25, 50]


21 - fruits = ['apple', 'mango', 'kiwi', 'banana']

Expected output : ['APPLE', 'MANGO', 'KIWI', 'BANANA']

22 - generate a new list that contains the lengths of each fruit's name.

fruits = ['apple', 'mango', 'kiwi', 'banana']

Expected output : [5, 5, 4, 6]

23 - Use list comprehension to create a new list, and then determine and print the
length of the new list.

fruits = ['apple', 'mango', 'kiwi', 'banana']

Expected output : 4

24 - make a list that contains each fruit with more than 5 characters

fruits = ['apple', 'mango', 'kiwi', 'banana']

Expected output : ['banana']

25 - make a list that contains each fruit with exactly 5 characters

fruits = ['apple', 'mango', 'kiwi', 'banana']

Expected output : ['apple', 'mango']

26 - Create a variable named capitalized_fruits ['Apple', 'Mango', 'Kiwi',


'Banana']

Expected output : ['Apple', 'Mango', 'Kiwi', 'Banana']

27 - Make a variable named even_numbers that holds only the even numbers

numbers = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 17, 19, 23, 256, -8, -4, -2, 5, -9]

Expected output : [2, 4, 6, 8, 10, 256, -8, -4, -2]

28 - Make a variable named odd_numbers that holds only the odd numbers

numbers = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 17, 19, 23, 256, -8, -4, -2, 5, -9]

Expected output : [3, 5, 7, 9, 11, 13, 17, 19, 23, 5, -9]

29 - test_list = [5,6,[],3,[],[],9] write a program to remove empty list from list

Expected output : [5, 6, 3, 9]

30 - mtlist =['101','102','103','515','117']

find those number whose first and last elements are same

Expected output : ['101', '515']


31 - Remove the dollar signs and convert strings to integers
fees = ['$120', '$250', '$300']

Expected output : 670

32 - list1 = ["Hello ", "class "]


list2 = ["world", "bdit"]

Expected output : ['Hello world', 'Hello bdit', 'class world', 'class bdit']

34 - Find all negative number

numbers = [7,-8, -4,9, -2, -9,2,39]

Expected output : [-8, -4, -2, -9]

35 - Find all of the numbers from 1-100 that have a 3 in them

Expected output : [3, 13, 23, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 53, 63,
73, 83, 93]

36 - Given numbers = range(10), produce a list containing the word ‘even’ if a


number in the numbers is even, and the word ‘odd’ if the number is odd.

Expected output : ['even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd',
'even', 'odd']

37 - Count the number of spaces in a string

string = "Hello world! This is a string with spaces."

Expected output : 7

38 - Remove vowel

w="hello"

Expected output : ['e', 'o']

You might also like