AttributeError: 'set' object has no attribute 'extend' in Python
Dung Do Tien Aug 16 2022 313
Hello everyone, I am a newbie in Python and today I have two lists that contain the same format data. I want to insert all data from the second list into the first list by using extend()
method. Here is my example:
lst_phone1 = {"Nokia 1080", "Nokia 860", "Vertu"}
lst_phone2 = {"Samsung galaxy", "Iphone 12", "RedMe 10"}
lst_phone1.extend(lst_phone2)
print(lst_phone1)
But I got an exception throw AttributeError: 'set' object has no attribute 'extend'. Here is detail of the exception:
Traceback (most recent call last):
File "main.py", line 3, in <module>
lst_phone1.extend(lst_phone2)
AttributeError: 'set' object has no attribute 'extend'
** Process exited - Return Code: 1 **
Press Enter to exit terminal
Anyone can explain and solve the issue to help me?
Thanks for any support.
Have 1 answer(s) found.
- D0
Dheeraj Yadav Aug 16 2022
You can see this error message tells us that we cannot use the method
extend()
on an object whose data type is a set. This is becauseextend()
is a list method. It is not supported by sets.If we want to merge our two sets, we have to use an addition sign:
lst_phone1 = {"Nokia 1080", "Nokia 860", "Vertu"} lst_phone2 = {"Samsung galaxy", "Iphone 12", "RedMe 10"} lst_phone1.update(lst_phone2) print(lst_phone1)
#Output
{'Vertu', 'RedMe 10', 'Iphone 12', 'Nokia 1080', 'Samsung galaxy', 'Nokia 860'} ** Process exited - Return Code: 0 ** Press Enter to exit terminal
Hope this answer can solve the problem for you.
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.