KnowledgeBoat Logo

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