Computer Science
How are the statements lst = lst + 3 and lst += [3] different, where lst is a list? Explain.
Python List Manipulation
38 Likes
Answer
The statement lst = lst + 3 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 integer. The statement lst += [3] will add 3 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
21 Likes
Related Questions
If a is [1, 2, 3]
- what is the difference (if any) between a * 3 and [a, a, a]?
- is a * 3 equivalent to a + a + a?
- what is the meaning of a[1:1] = 9?
- what's the difference between a[1:2] = 4 and a[1:1] = 4?
What's the purpose of the del operator and pop method? Try deleting a slice.
What's a[1 : 1] if a is a list of at least two elements? And what if the list is shorter?
How are the statements lst += "xy" and lst = lst + "xy" different, where lst is a list? Explain.