+ 3
Hi Sololearnersâ„ïž, pls what are the differences between append(), insert(), add() in Python
3 Answers
+ 6
list.append (element) â append element to the end of the list. More information: https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_list_append.asp . array.array, collections.deque, xml.etree.ElementTree.Element in the standard library have the same method which does the same.
list.insert (position, element) â insert the element to the specified position. For example: a = [0, 5, 8]; a.insert(1, 3); # now a is [0, 3, 5, 8]. More information: https://www.w3schools.com/python/ref_list_insert.asp . array.array, collections.deque, xml.etree.ElementTree.Element in the standard library have the same method which does the same.
set.add (element) â add the element to the set, if it does not already exist there. set uses this method because it that way is more close to mathematical sets and sets have no element order (actually it is random). More information: https://www.w3schools.com/python/ref_set_add.asp
+ 4
append() method add a given element at the end of selected list and the difference between append() and insert is that you can choose your element place to add. in the end add() method adds a given element to a set in a situation that it doesn't exist or present there.
For example:
a = ["a"]
print(a.append("b"))
print(a.insert(0,"b"))
>>>["a","b"]
>>>["b","a"]
+ 4
Thanks