List multiple in Python

Last modified: February 04, 2021

How to Append Multiple Items to List in Python

In today's tutorial, we'll learn how to append multiple items to a list.

Contents

  • 1. Using the append() method
  • 2. Using '+' operator
  • 3. Using extend() method
  • 4. Real example

1. Using the append() method

The append() method adds a single element to an existing list.
To add multiple elements, we need:

1. iterate over the elements list.
2. append each element.

Let's see the example:

my_list = ["DJANGO"] for i in ["PYTHON", "PHP"]: my_list.append(i) print(my_list)

So, in the above code, we try to append ["PYTHON", "PHP"] to my_list by:

1. iterate over ["PYTHON", "PHP"].
2. append element to my_list using the append() method.

output

['DJANGO', 'PYTHON', 'PHP']

2. Using '+' operator

Another way is using the '+' operator.

my_list = ["DJANGO"] new_list = my_list + ["PYTHON", "PHP"] #print print(new_list)

output

['DJANGO', 'PYTHON', 'PHP']

3. Using extend() method

The extend() method adds list elements to the current list.

my_list = ["DJANGO"] #append to the list my_list.extend(["PYTHON", "PHP"]) #print print(my_list)

output

['DJANGO', 'PYTHON', 'PHP']

Real example

Now, I'll give you an example in which we'll split a string into a list and append the list to an existing list.

#my list my_list = ["DJANGO"] #my string my_string = "Hello PYTHON and PHP" #Split the String split_s = my_string.split(' ') #output ['Hello', 'PYTHON', 'and', 'PHP'] #append the result to my_list my_list.extend(split_s) print(my_list)

output

['DJANGO', 'Hello', 'PYTHON', 'and', 'PHP']

  • turn a List to a Tuple in python
  • count items in list matching criteria or condition in python
  • How to define an empty list in python
  • Sorting a List in Python with example

Recent Tutorials:

  • Python: Various methods to remove the newlines from a text file
  • How to Check if String Contains a Newline in Python
  • 3 Methods to Check if String Starts With a Number in Python
  • How to get the Meaning of a Word in Python
  • How to solve IndexError list index out of range
  • Python: Test Internet Speed (Download - Upload)
List multiple in Python
report this ad
  • How to Properly Check if a Variable is Not Null in Python
  • Python Check if Value Exists in List
  • How to Properly Check if a Variable is Empty in Python
  • How to Break out a loop in Python
  • How to Check Type of Variable in Python
  • How to Properly Check if a Dictionary is Empty in Python