+ 2

What is the difference between Insert and extend method in list in python

What is the difference between Insert and extend method in list in python

9th Apr 2019, 8:11 PM
ankit goyal
ankit goyal - avatar
1 ответ
+ 1
list.insert(i, x) inserts an item "x" at a given position "i" in the list. list.extend(iterable) appends all the items from "iterable" to the list. For example: lst = [1, 2, 4] lst.insert(2, 3) # inserts the number 3 at the index 2 print(lst) # [1, 2, 3, 4] lst.extend([5, 6, 7]) print(lst) # [1, 2, 3, 4, 5, 6, 7]
9th Apr 2019, 8:48 PM
Diego
Diego - avatar