Computer Science
How are the statements lst += "xy" and lst = lst + "xy" different, where lst is a list? Explain.
Python List Manipulation
26 Likes
Answer
The statement lst = lst + "xy" will give error as + operator in Python requires that both its operands should be of the same type but here one operand is list and other is string. The statement lst += "xy" will add 'x' and 'y' at the end of the lst as += when used with lists requires the operand on the right side to be an iterable and it will add each element of the iterable to the end of the list.
Answered By
17 Likes
Related Questions
What's a[1 : 1] if a is a list of at least two elements? And what if the list is shorter?
What does each of the following expressions evaluate to? Suppose that L is the list
["These", ["are", "a", "few", "words"], "that", "we", "will", "use"].- L[1][0::2]
- "a" in L[1][0]
- L[:1] + L[1]
- L[2::2]
- L[2][2] in L[1]
How are the statements lst = lst + 3 and lst += [3] different, where lst is a list? Explain.
What's the purpose of the del operator and pop method? Try deleting a slice.