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]

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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]

[/bg_collapse]


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]

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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]

[/bg_collapse]


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

Given List:

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

Expected Output:

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

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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']

[/bg_collapse]


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']

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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']

[/bg_collapse]


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 '

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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 

[/bg_collapse]


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])

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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']

[/bg_collapse]


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

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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

[/bg_collapse]


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

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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]

[/bg_collapse]


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

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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]

[/bg_collapse]



dinamobet - dinamobet giriş - dinamobet güncel giriş
dinamobet
dinamobet - dinamobet giriş - dinamobet güncel giriş
dinamobet
dinamobet
dinamobet
Dinamobet
betasus
meritking
dinamobet
dinamobet
dinamobet
dinamobet
dinamobet
dinamobet
dinamobet
Dinamobet
Dinamobet
Dinamobet
Dinamobet
Dinamobet
Dinamobet
Kulisbet
Kulisbet
Dinamobet
Dinamobet
Kulisbet
Kulisbet
Kulisbet giriş
Kulisbet güncel giriş