Insert, append, extend and concatanate lists in Python
Insert a new element in the list at a given index
>>> myList = [10,20,30,50]
>>> myList.insert(3,40)
>>> myList
[10, 20, 30, 40, 50]
Append a new element to the list
>>> myList=[1,2,3]
>>> myList.append(4)
>>> myList
[1, 2, 3, 4]
>>> myList=[1,2,3]
>>> myList.append([4,5])
>>> myList
[1, 2, 3, [4, 5]]
Extend a list with another list
>>> myList=[1,2,3]
>>> myList.extend([4,5])
>>> myList
[1, 2, 3, 4, 5]
Concatanate lists
>>> myList=[1,2,3]
>>> myList=myList+[4,5]
>>> myList
[1, 2, 3, 4, 5]
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> x + y
[1, 2, 3, 4, 5, 6]
Duplicate a list
myList=[1,2]
>>> myList*3
[1, 2, 1, 2, 1, 2]
See also
Last update : 10/26/2019