Question 1 : Multiply every element of the given list by 2.
Given List:
Expected Output:
Show 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:
Question 2 : Remove all occurrence of a given item from the list.
Given List:
Item to Remove: 10
Expected Output:
Show 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:
Question 3 : Remove all the empty strings from the list.
Given List:
["Hello","Logical","","","Python"]
Expected Output:
['Hello', 'Logical', 'Python']
Show 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']
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']
Show 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']
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:
Show 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:
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])
Show 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\
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.
Show 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:
Question 8 : Write a program to swap even elements of the list with the odd elements.
Show 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:
Question 9 : Write a program to fetch positive and negative numbers from the list.
Show 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: