DSA Questions on Lists using Python

DSA Questions on Lists using Python

Question 1 :

What will be the output of the following code block?

a=[1,2,3,4,5,6,7,8,9]
a[::2]=10,20,30,40,50,60
print(a)        

ValueError: attempt to assign sequence of size 6 to extended slice of size 5

Answer : Value Error: attempt to assign sequence of size 6 to extended slice of size 5

Explanation: The code block provided here contains a syntax error. The slice assignment a[::2] = 10, 20, 30, 40, 50, 60 should have the same number of elements on both sides, but it doesn't. The slice a[::2] selects every second element of the list a, which results in a list of 5 elements: [1, 3, 5, 7, 9]. On the right side of the assignment, there are 6 elements: 10, 20, 30, 40, 50, 60. Since the number of elements is not equal, Python will raise a ValueError when executing this code:

ValueError: attempt to assign sequence of size 6 to extended slice of size 5

To fix the error, we should ensure that the number of elements on both sides of the assignment is the same. For example, we can change the right side of the assignment to have only 5 elements:

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
a[::2] = 10, 20, 30, 40, 50
print(a)        

The output of the corrected code block will be:

[10, 2, 20, 4, 30, 6, 40, 8, 50]

************************************************************************************

Question 2 :

What will be the output of the following code block?

a=[1,2,3,4,5]
print(a[3:0:-1])        

Answer : [4, 3, 2]

Explanation : The given code block performs a slice operation on the list a using the syntax a[start:stop:step]. In this case, start is 3, stop is 0, and step is -1. This slice operation creates a new list containing elements from the original list, starting at index 3, going until index 0 (not inclusive), and selecting every element with a step of -1 (moving in reverse).

So, the output of the code block will be:

[4, 3, 2]        

The slice operation picks the elements at indices 3, 2, and 1 from the original list.

************************************************************************************

Question 3 :

What will be the output of the following code snippet?

def f(value, values):    
      v = 1    
      values[0] = 44
t = 3
v = [1, 2, 3]
f(t, v)
print(t, v[0])        

Answer : 3 44

Explanation: In the given code snippet, the function f takes two arguments, value and values. Inside the function, it assigns 1 to a local variable v and sets the first element of the list values to 44. The function doesn't have a return statement, so it implicitly returns None.

Here's a step-by-step explanation of the code execution:

  1. The variable t is assigned the value 3.
  2. The variable v is assigned the list [1, 2, 3].
  3. The function f is called with arguments t and v. The parameter value takes the value of t (3), and values takes the value of v ([1, 2, 3]).
  4. Inside the function, the local variable v is assigned the value 1. This does not affect the global variable v.
  5. The first element of the list values (which is the same object as the global variable v) is set to 44, so the list values (and also the global variable v) becomes [44, 2, 3].
  6. The function ends without a return statement, implicitly returning None.
  7. The print() function is called with arguments t and v[0], which are 3 and 44, respectively.

So, the output of the code snippet will be:

3 44        

***********************************************************************************

Question 4 :

What will be the output of the following code block?

fruit_list1 = ['Apple', 'Berry', 'Cherry', 'Papaya']
fruit_list2 = fruit_list1
fruit_list3 = fruit_list1[:] 
fruit_list2[0] = 'Guava'
fruit_list3[1] = 'Kiwi' 
sum = 0
for ls in (fruit_list1, fruit_list2, fruit_list3):  
    if ls[0] == 'Guava':        
        sum += 1    
    if ls[1] == 'Kiwi':        
        sum += 20 print(sum)
print(sum)        

Answer : 22

Explanation: Here's a step-by-step explanation of the code block:

  1. fruit_list1 is defined with four fruit names.
  2. fruit_list2 is assigned the reference of fruit_list1, so both lists now point to the same memory location. Any changes made to either of them will be reflected in the other.
  3. fruit_list3 is assigned a slice of fruit_list1 that includes all elements. This creates a new, shallow copy of the list, so changes made to fruit_list3 won't affect fruit_list1 or fruit_list2.
  4. The first element of fruit_list2 is changed to 'Guava'. Since fruit_list1 and fruit_list2 refer to the same list, the first element of fruit_list1 is also changed to 'Guava'.
  5. The second element of fruit_list3 is changed to 'Kiwi'. This does not affect fruit_list1 or fruit_list2.
  6. A variable sum is initialized with the value 0.
  7. A for loop iterates over a tuple containing fruit_list1, fruit_list2, and fruit_list3.If the first element of the current list is 'Guava', sum is incremented by 1.If the second element of the current list is 'Kiwi', sum is incremented by 20.
  8. The final value of sum is printed.

After the modifications, the lists are as follows:

  • fruit_list1: ['Guava', 'Berry', 'Cherry', 'Papaya']
  • fruit_list2: ['Guava', 'Berry', 'Cherry', 'Papaya'] (same as fruit_list1)
  • fruit_list3: ['Apple', 'Kiwi', 'Cherry', 'Papaya']

Now let's calculate the sum:

  • For fruit_list1: sum += 1 (first element is 'Guava') = 1
  • For fruit_list2: sum += 1 (first element is 'Guava') = 2
  • For fruit_list3: sum += 20 (second element is 'Kiwi') = 22

So, the output of the code block will be:

22

************************************************************************************

Question 5 :

What will be the output of the following code block?

arr = [[1, 2, 3, 4],      
           [4, 5, 6, 7],    
            [8, 9, 10, 11],  
             [12, 13, 14, 15]]
for i in range(0, 4):   
print(arr[i].pop())        

Answer : 4 7 11 15

Explanation: In the given code block, we have a 2D list called arr. The code iterates through the rows of the 2D list using the for loop with a range from 0 to 3 (4 not included). In each iteration, the pop() method is called on the current row (i.e., arr[i]), which removes and returns the last element of the row.

Here's the step-by-step execution of the code:

  1. i = 0: The last element of the first row (arr[0]) is 4. The pop() method removes and returns 4, and the first row becomes [1, 2, 3]. The print() function outputs 4.
  2. i = 1: The last element of the second row (arr[1]) is 7. The pop() method removes and returns 7, and the second row becomes [4, 5, 6]. The print() function outputs 7.
  3. i = 2: The last element of the third row (arr[2]) is 11. The pop() method removes and returns 11, and the third row becomes [8, 9, 10]. The print() function outputs 11.
  4. i = 3: The last element of the fourth row (arr[3]) is 15. The pop() method removes and returns 15, and the fourth row becomes [12, 13, 14]. The print() function outputs 15.

So, the output of the code block will be: 4 7 11 15

************************************************************************************

Question 6 :

What will be the output of the following code block?

a=[1,2,3,4,5,6,7,8,9]
print(a[::2])        

Answer : [1,3,5,7,9]

Explanation: In the given code block, a slice operation is performed on the list a. The slice operation uses the syntax a[start:stop:step]. In this case, start is not specified (defaults to 0), stop is not specified (defaults to the end of the list), and the step is 2. The slice operation creates a new list containing elements from the original list, starting at the beginning, going until the end, and selecting every second element (using a step of 2).

So, the output of the code block will be:

[1, 3, 5, 7, 9]

The slice operation picks the elements at indices 0, 2, 4, 6, and 8 from the original list.

**********************************************************************************

Question 7 :

What is the correct command to shuffle the following list?

fruit=['apple', 'banana', 'papaya', 'cherry']

Answer : random.shuffle(fruit)

Explanation: To shuffle a list in Python, you can use the shuffle() function from the random module. Here's the correct command to shuffle the fruit list:

import random fruit = ['apple', 'banana', 'papaya', 'cherry']random.shuffle(fruit)        

Now, the fruit list will be shuffled randomly. Note that the shuffle() function shuffles the list in place, meaning it modifies the original list rather than returning a new shuffled list.

*******************************************************************************

Question 8 :

What will be the output of the following code block?

data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
def fun(m):    v = m[0][0]     
for row in m:        
for element in row:            
if v < element: v = element     
return vprint(fun(data[0]))        

Answer : 4

Explanation: In the given code block, the function fun takes a 2D list (matrix) as an argument and iterates through its elements to find the largest value. The code block calls the function fun with the first element of the data list, which is [[1, 2], [3, 4]].

Here's a step-by-step explanation of the code execution:

  1. The function fun is called with the argument [[1, 2], [3, 4]].
  2. The variable v is assigned the value of the first element in the first row of the matrix, which is 1.
  3. The function iterates through each row of the matrix and each element in each row using nested loops.
  4. If the current element is greater than the current value of v, v is updated with the new larger value.
  5. After iterating through all the elements, v holds the largest value in the matrix.
  6. The function returns the largest value found, which is 4.

So, the output of the code block will be:

4        

*************************************************************************************

Question 9 :

What will be the output of the following code block?

arr = [1, 2, 3, 4, 5, 6]for i in range(1, 6):    
arr[i - 1] = arr[i]for i in range(0, 6):     
print(arr[i], end = " ")        

Answer : 2 3 4 5 6 6

**********************

Explanation: The given code block first modifies the arr list using a for loop and then prints the elements of the modified list. Here's the step-by-step execution of the code:

  1. Iterate through the list using a for loop with a range from 1 to 5 (6 not included). For each i, the value of arr[i] is assigned to arr[i - 1]. This operation effectively shifts each element to the left by one position except for the first element. After the loop, arr becomes: [2, 3, 4, 5, 6, 6]
  2. Iterate through the modified list using another for loop with a range from 0 to 5 (6 not included). The print() function is called with the end = " " parameter, which prints each element followed by a space instead of a newline.

So, the output of the code block will be:

2 3 4 5 6 6         

############################################################

To view or add a comment, sign in

More articles by ARNAB MUKHERJEE 🇮🇳

Insights from the community

Others also viewed

Explore topics