Informatics Practices
Consider the following lists:
L1 = ["this", "is", "a", "list"]
L2 = ["this", "is", "another list"]
Which of the following statements will result in an error?
- L1 = L2
- L1.copy()
- L2.append(1, 2, 3)
- L2.pop()
Answer
L2.append(1,2,3)
Reason — The append() method in list takes one argument and adds it as a single element to the end of the list. Using L2.append(1, 2, 3) will result in an error because it provides multiple arguments. The correct statement would be L2.append([1, 2, 3]) to add the entire list [1, 2, 3] as a single element to L2.
Related Questions
Select the output of the following expression:
str1 = "pen" print(list(str1))- ['p', 'e', 'n' ]
- [pen]
- [p/e/n]
- { "pen" }
The sequential accessing of each of the elements in a list is called:
- List Indexing
- List Traversal
- List Slicing
- List Accessing
Consider the following code:
>>>A = [10, 20, 30, 40] >>>B = A.copy() >>>A[2] = 50 >>>BWhich of the following will be the elements of list A and B?
- A = [10, 20, 50, 40]
B = [10, 20, 30, 40] - A = [10, 20, 30, 40]
B = [10, 20, 50, 40] - A = [10, 20, 30, 40, 50]
B = [10, 20, 50, 40] - A = [10, 20, 40]
B = [10, 20, 50, 40]
- A = [10, 20, 50, 40]
Consider the following lists:
A = [1,2] B = [1,2]What will be the output of the following?
print(A==B)- True
- False
- Error
- No output