add vs update in set operations in python

前端 未结 9 642
天涯浪人
天涯浪人 2020-12-07 18:30

What is the difference between add and update operations in python if i just want to add a single value to the set.

a = set()
a.update([1]) #works
a.add(1) #         


        
9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-07 19:11

    add method directly adds elements to the set while the update method converts first argument into set then it adds the list is hashable therefore we cannot add a hashable list to unhashable set.

提交回复
热议问题