Add Multiple Elements to List in Python Example

29-Nov-2022

.

Admin

Add Multiple Elements to List in Python Example

Hi Dev,

I will explain step by step tutorial add multiple elements to list in python example. I would like to share with you how to add multiple numbers to a list in python. I would like to share with you python add items of list to list. We will use python list insert multiple elements.

There are many ways to add multiple items to a python list. i will give you three examples using extend() method, append() method and concat to insert multiple elements to python list. so let's see the below examples.

so let's see following examples with output:

Example 1:


main.py

myList = [1, 2, 3, 4]

# Add new multiple elements to list

myList.extend([5, 6, 7])

print(myList)

Output:

[1, 2, 3, 4, 5, 6, 7]

Example 2:

main.py

myList = [1, 2, 3, 4]

# Add new multiple elements to list

myList.append(5)

myList.append(6)

myList.append(7)

print(myList)

Output:

[1, 2, 3, 4, 5, 6, 7]

Example 3:

main.py

myList = [1, 2, 3, 4]

myList2 = [5, 6, 7]

# Add new multiple elements to list

newList = myList + myList2

print(newList)

Output:

[1, 2, 3, 4, 5, 6, 7]

#Python