+ 3

Hi Sololearners♄, pls what are the differences between append(), insert(), add() in Python

26th Mar 2021, 6:13 AM
Oladimeji Elijah
Oladimeji Elijah - avatar
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
26th Mar 2021, 6:27 AM
#0009e7 [get]
#0009e7 [get] - avatar
+ 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"]
26th Mar 2021, 8:21 AM
Anita Rostam Aliloo
Anita Rostam Aliloo - avatar
+ 4
Thanks
27th Mar 2021, 6:55 PM
Oladimeji Elijah
Oladimeji Elijah - avatar