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