Computer Science
Predict the output.
b = [[9, 6], [4, 5], [7, 7]]
x = b[:2]
x[1].append(10)
print(x)
Answer
[[9, 6], [4, 5, 10]]
Working
b = [[9, 6], [4, 5], [7, 7]]— This line initializes a listbcontaining three sublists, each containing two elements.x = b[:2]— This line creates a new listxby slicing the listbfrom index 0 to index 1. So,xwill contain the first two sublists ofb. At this point,xwill be[[9, 6], [4, 5]].x[1].append(10)— This line accesses the second sublist ofx, which is[4, 5], and appends 10 at its end. Nowxbecomes[[9, 6], [4, 5, 10]]print(x)— This line prints the listx.
Related Questions
Predict the output :
def even(n): return n % 2 == 0 list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] ev = [n for n in list1 if n % 2 == 0] evp = [n for n in list1 if even(n)] print(evp)Predict the output.
b = [[9, 6], [4, 5], [7, 7]] x = b[:2] x.append(10) print(x)Find the Error. Consider the following code, which runs correctly at times but gives error at other times. Find the error and its reason.
Lst1 = [23, 34, 12, 77, 34, 26, 28, 93, 48, 69, 73, 23, 19, 88] Lst2 = [] print("List1 originally is: ", Lst1) ch = int(input("Enter 1/2/3 and \ predict which operation was performed?")) if ch == 1: Lst1.append(100) Lst2.append(100) elif ch == 2: print(Lst1.index(100)) print(Lst2.index(100)) elif ch == 3: print(Lst1.pop()) print(Lst2.pop())Suggest the correction for the error(s) in previous question's code.