Loading samples...
Loading samples...
py-sample-9
Master Python lists - indexing, slicing, methods, and comprehensions
Which method adds item at end of list?
What is [1,2,3] + [4,5]?
Which removes item by value?
What does L.pop() do?
Which creates shallow copy?
What is [0] * 3?
Which sorts list in-place?
What does enumerate(L) return?
Which inserts at specific index?
What is list('abc')?
Explain the difference between append() and extend() with examples.
[3 marks]What is list comprehension? Write syntax and give an example.
[3 marks]Differentiate between sort() and sorted() functions.
[3 marks]Explain shallow copy vs deep copy with examples.
[3 marks]What are list methods for stack operations? Explain LIFO.
[3 marks]Explain how to use list as a queue. What are the performance implications?
[3 marks]What is slicing assignment? How to replace part of a list?
[3 marks]Explain the zip() function with lists and unpacking.
[3 marks]What are all() and any() functions? When are they useful with lists?
[3 marks]Explain list slicing with negative indices and step parameter.
[3 marks]Write a program to find second largest and second smallest elements.
[5 marks]Create a program to flatten a nested list of arbitrary depth.
[5 marks]Write a program to remove all duplicates while preserving order.
[5 marks]Create a program to rotate list elements left/right by k positions.
[5 marks]Write a program to find all pairs in list that sum to target.
[5 marks]Create a program implementing custom list filter using list comprehension.
[5 marks]Predict output: a = [1, 2, 3] b = a b.append(4) print(a)
[2 marks]Explanation:b references the same list as a. Modifying b affects a too.
Find error: my_list = [1, 2, 3] print(my_list[3])
[2 marks]Explanation:Valid indices are 0, 1, 2. Index 3 doesn't exist in a 3-element list.
Predict output: L = [3, 1, 4, 1, 5] print(L.count(1)) print(L.index(4))
[2 marks]Explanation:count(1) finds two 1s. index(4) returns first position of 4, which is at index 2.
Find error: L = [1, 2, 3] L = L.append(4) print(L)
[2 marks]Explanation:append() returns None. L becomes None. Should be L.append(4) without assignment.
Predict output: x = [i**2 for i in range(5)] print(x)
[2 marks]Explanation:List comprehension squares each number from 0 to 4.
Find error: L = [[1, 2], [3, 4]] M = L.copy() M[0][0] = 99 print(L)
[3 marks]Explanation:copy() is shallow. Nested lists are still references. Use deepcopy() for full independence.
Predict output: L = [1, 2, 3, 4, 5] print(L[1:4:2]) print(L[::-1])
[3 marks]Explanation:[1:4:2] starts at 1, stops before 4, step 2 → indices 1,3 → [2,4]. [::-1] reverses.
Find error: for i in range(len(L)): if L[i] % 2 == 0: L.remove(L[i])
[2 marks]Explanation:Modifying list while iterating by index causes issues. List shrinks but index increases.