Python List Exercise and Solutions


Question 1 : Multiply every element of the given list by 2.

Given List:

[1,2,3,4,5]

Expected Output:

[2, 4, 6, 8, 10]

There are three ways by which we can perform this task:

1. Using for loop.

my_list = [1,2,3,4,5]

new_list = []

for x in my_list:
    new_list.append(x*2)


print(new_list)

2. Using list comprehension.

my_list = [1,2,3,4,5]

new_list = [x*2 for x in my_list]

print(new_list)

3. Using map() functions.

my_list = [1,2,3,4,5]

new_list = map((lambda x: x*2), my_list)

print(list(new_list))

Output:

[2, 4, 6, 8, 10]


Question 2 : Remove all occurrence of a given item from the list.

Given List:

[0,10,2,10,4,10]

Item to Remove: 10

Expected Output:

[0, 2, 4]

To achieve this, you can use the list comprehension.

my_list = [0,10,2,10,4,10]

item_to_remove = 10

final_list = [i for i in my_list if i!= item_to_remove]

print(final_list)

Output:

[0, 2, 4]


Question 3 : Remove all the empty strings from the list.

Given List:

["Hello","Logical","","","Python"]

Expected Output:

['Hello', 'Logical', 'Python']

Three ways by which we can remove the empty string from the list are:

1. Using for loop.

my_list = ["Hello","Logical","","","Python"]

new_list = []
for i in my_list:
    if i != "":
        new_list.append(i)

print(new_list)

2. Using list comprehension.

my_list = ["Hello","Logical","","","Python"]

new_list = [i for i in my_list if i != ""]

print(new_list)

3. Using filter() function.

my_list = ["Hello","Logical","","","Python"]

new_list = list(filter(None, my_list))

print(new_list)

Output:

['Hello', 'Logical', 'Python']


Question 4 : Append two lists in the given way

Given List:

my_list1 = ['I', 'Logical']
my_list2 = ['Love', 'Python']

Expected Output:

['I Love', 'I Python', 'Logical Love', 'Logical Python']

Two ways to append given list in the above way:

1. Using nested for loop.

my_list1 = ['I', 'Logical']
my_list2 = ['Love', 'Python']
final_list = []

for i in my_list1:
    for j in my_list2:
        final_list.append(i + " " + j)
        
print(final_list)

2. Using list comprehension.

my_list1 = ['I', 'Logical']
my_list2 = ['Love', 'Python']
final_list = [i +" "+ j for i in my_list1 for j in my_list2]
        
print(final_list)

Output:

['I Love', 'I Python', 'Logical Love', 'Logical Python']

Question 5 : Concatenate two lists

In this exercise, you have to concatenate two lists index wise.

Given List:

my_list1 = ['I', 'Logical']
my_list2 = ['Love', 'Python']

Expected Output:

'I Love Logical Python '

Using zip() function to concatenate two lists.

my_list1 = ['I', 'Logical']
my_list2 = ['Love', 'Python']

new_list  = [(i +" "+ j + " ") for i, j in zip(my_list1, my_list2)]

print("".join(new_list))

Output:

I Love Logical Python 

Question 6 : Consider the below list and answer the following questions:

Given List:

about_you = ["You", ["are", "the", "best"], "will", "win", "in", "any", "case"]

What is the output of the below code blocks?

1. 
print(about_you[1])
print(about_you[1:2])
print(about_you[1:2][0])
print(about_you[1:2][0][0])
print(about_you[1:2][1])
2. 
print("are" in about_you)
3. 
print("are" in about_you[1])
4. 
print(about_you[0] + about_you[1])
5. 
print([about_you[0]] + about_you[1])

Answer:

1. 
['are', 'the', 'best']
[['are', 'the', 'best']]
['are', 'the', 'best']
are
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-16-90479685698a> in <module>
      3 print(about_you[1:2][0])
      4 print(about_you[1:2][0][0])
----> 5 print(about_you[1:2][1])

IndexError: list index out of range\
2. 
False
3.
True
4.
TypeError                                 Traceback (most recent call last)
<ipython-input-19-1fb54a30aa4b> in <module>
----> 1 print(about_you[0] + about_you[1])

TypeError: can only concatenate str (not "list") to str
5.
['You', 'are', 'the', 'best']

Question 7 : Write a program to find the smallest and largest integer from the list of integers.

Without using min/max:

lst = [5,3,9,1,2,7,6]
lst.sort()
print("Smallest = ", lst[0])
print("Largest = ", lst[-1])

Using min/max:

lst = [5,3,9,1,2,7,6]
print("Smallest = ", min(lst))
print("Largest = ", max(lst))

Output:

Smallest =  1
Largest =  9

Question 8 : Write a program to swap even elements of the list with the odd elements.

Solution:

lst = [1,2,3,4,5,6]
lst_length = len(lst)

if lst_length%2 == 1:
    lst_length -= 1

for i in range(0,lst_length,2):
    lst[i], lst[i+1] = lst[i+1], lst[i]

print(lst)

Output:

[2, 1, 4, 3, 6, 5]

Question 9 : Write a program to fetch positive and negative numbers from the list.

Solution:

lst = [1,-2,-3,4,-5,6]
positive = []
negative = []

for i in lst:
    if i > 0:
        positive.append(i)
    if i < 0:
        negative.append(i)
        
print(positive)
print(negative)

Output:

[1, 4, 6]
[-2, -3, -5]