In Python or any other programming language, you must work with a list data structure that allows you to store more than one similar or unique item. Generally, you are introduced to the basics of lists with the addition of items at the start or end of the data structure. How to add multiple items to a list in Python is usually forgotten. In this article, we will see if it is possible to do that and the steps you can take.

So, can you add multiple items to a list in Python? You can add multiple items to a list in Python in a couple of ways, including the use of

  1. extend() function
  2. Concatenation of the two lists using the += operator
  3. For loop and list.append() function
  4. The * unpacking operator in Python
  5. The itertools.chain function, which is more efficient
  6. List slicing to add multiple items in a list at the start, end, or between

Generally, you would add single values to a list using the append() function. However, there are times when you will be required to add multiple items to a list, especially when using range values in Python.

How to append multiple items to a list in Python

There are five ways to add multiple items to a list in Python:

  1. By using the extends() function
  2. By concatenating the two lists using the += operator
  3. By using for loop and list.append() function
  4. By using the * unpacking operator in Python
  5. By using the itertools.chain function (more efficient)
  6. By using list slicing to add multiple items in a list at the start, end, or between.

Let’s see, in detail, how to work with the following approaches to add multiple items to a Python list.

How to append multiple items to a list using the extend() function

The most common way to add more than one value to a list at once is to use Python’s built-in function, extend(). The syntax used in the function is: list.extend([values you want to add in a list]).

Here’s an example of adding multiple items to a list in Python using the list.extend() function:

So, you have a list

my_list = [1, 2, 3, 4, 5]

and you want to add the following values: 6, 7, 8, 9, 10. You can use the list.extend() to add these values to a list at once.

my_list.extend([6, 7, 8, 9, 10])

After printing the list

print(my_list)

Results:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
"""
@author: hoofhoof
"""

my_list = [1, 2, 3, 4, 5]
my_list.extend([6, 7, 8, 9, 10])

print(my_list)

You may also use range value or input from other functions in your code

list2 = [1, 2, 3, 4]

list2.extend(range(5, 11))
print(list2)

We achieve the same results:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Here’s another example using strings:

alphabets = ['a', 'b', 'c', 'd', 'e', 'f']
alphabets.extend(['g', 'h'])

print(alphabets)

Results:

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

See, adding multiple items to a Python list is as easy as that.

Now, let’s see other ways to append multiple items to a list in Python.

How to append multiple items to a list by using the list concatenation with the += operator

You can use list concatenation (+= operator) to join an existing list with another list containing the multiple values you want to add. Here’s how to concatenate two lists in Python:

Let’s say you have a list

my_list = [1, 2, 3, 4]

You can add multiple items to the list by creating another list and concatenating them together.

items_to_add = [5, 6, 7, 8]

Concatenate the two lists using the += operator

my_list += items_to_add

Output

print(my_list)

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

How to append multiple items to a list by using for loop and append()

You can loop through a list containing the items you want to add to a new list. With each iteration, you can append the single value found in the current index to the list you intend to add multiple values.

Here’s how to use for loop to add multiple items to a list:

So, you have multiple items you want to add to a list

values_to_add = [5, 6, 7, 8]

and you have an existing list that you want to add the values to

my_list = [1, 2, 3, 4]

You can use the for loop in the following approach

for item in values_to_add:
    my_list.append(item)
    print(f"Appended: {item} to my_list list")
    

print(my_list)

Output

Appended: 5 to my_list list
Appended: 6 to my_list list
Appended: 7 to my_list list
Appended: 8 to my_list list
[1, 2, 3, 4, 5, 6, 7, 8]
my_list = [1, 2, 3, 4]

values_to_add = [5, 6, 7, 8]

for item in values_to_add:
    my_list.append(item)
    print(f"Appended: {item} to my_list list")
    

print(my_list)

How to append multiple items to a list by using the * unpacking operator

You may also use the * operator to unpack the multiple values you want to add to your list. Here’s an example of how you would do that:

Let’s say you have a list of values you want to add to another list

values_to_add = [4, 5, 6, 7, 8, 9, 10]

You can use list unpacking to unpack multiple values to a list in Python in the following way:

my_list = [1, 2, 3, *values_to_add]

If you print your new list, you should have all the new values appended

print(my_list)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
"""
@author: hoofhoof
"""




values_to_add = [4, 5, 6, 7, 8, 9, 10]


my_list = [1, 2, 3, *values_to_add]



print(my_list)

How to append multiple items to a list more efficiently with itertools.chain

Itertools Python module provides the chain() function that can be used to add multiple items to a list.

Here’s how to use itertools.chain to add multiple values to a list at once:

from itertools import chain

my_list = [1, 2, 3]

my_list += chain([4, 5, 6, 7, 8])

print(my_list)

Output

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

How to append multiple items to a list through list slicing

The final approach to appending multiple items to a list in Python is to implement list slicing. You may use list slicing to append items to a list at the end.

You may also add multiple items to a list at the start or between.

Here’s an example of list slicing to add more than one item to a list at once

You have your initial list.

my_list = [1, 2, 3, 4, 5]

You can add multiple values to the list using list slicing and adding your new items at the end of the list.

my_list[len(my_list):] = [6, 7, 8, 9, 10]

Output

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_list = [1, 2, 3, 4, 5]
 
my_list[len(my_list):] = [6, 7, 8, 9, 10]



print(my_list)

Example of application of appending multiple items to a list

You may have a file or a string that you may want to extract names of people by splitting the string based on spaces or commas.

Sometimes, the names can be entered and finally formatted wrongly when performing data entry. You want to extract names of people from a string that was maybe entered wrongly or missing some characters such as a comma (,).

errored_names = "Jane Doe John"
corrected_names = errored_names.split(' ')

And then adding the corrected names to another list containing the correct names

attendees = ['Anne', 'Mary']
attendees.extend(corrected_names)
print(attendees)

Output

['Anne', 'Mary', 'Jane', 'Doe', 'John']

Related Questions

What is the difference between appending a list and extending a list?

Appending to a list involves adding only one item at the end of a list. Extending a list involves adding one or more multiple items to an existing list in Python.

Can you append objects to a list in Python?

You can append objects to a list in Python. As an object is an instance of a class, you can create multiple instances of a class and add them to a list.

As an example, here’s how you can append ‘student’ objects of a class Student to a list in Python:

This is the Student class

class Student(): 

    def __init__(self, student_name, student_age): 

        self.student_name = student_name
        self.student_age = student_age  
        
    def student_info(self):
        print(self.student_name, self.student_age)

These are the students we may create with the class above to produce multiple objects we can store in a list:

students = {"Steve": 20, "Mary": 19, "Jane": 18, "Wagio": 21, "Wanjiku": 23}

Let’s initiate a list

list_of_students = [] 

Use a for loop to loop through the students in the dictionary, create each student object, and add the object to the list we initiated

for student_name, student_age in students.items():
    list_of_students.append(Student(student_name, student_age)) 
    

Here, we have appended a couple of objects we have created with the Student class to a Python list.

If you check the last item on our list, it is an object:

print(type(list_of_students[-1]))


<class '__main__.Student'>

And if we loop through the list, you can see all the student objects stored in our list

for student in list_of_students:
   student.student_info()
       
    

Results:

Steve 20
Mary 19
Jane 18
Wagio 21
Wanjiku 23
class Student(): 

    def __init__(self, student_name, student_age): 

        self.student_name = student_name
        self.student_age = student_age  
        
    def student_info(self):
        print(self.student_name, self.student_age)
    
list_of_students = [] 

students = {"Steve": 20, "Mary": 19, "Jane": 18, "Wagio": 21, "Wanjiku": 23}

for student_name, student_age in students.items():
    list_of_students.append(Student(student_name, student_age))
    
    
print(type(list_of_students[-1]))

for student in list_of_students:
   student.student_info()
       
    

Here, we have added multiple objects to a list at once using the for loop and append method. You can see that this is another application for adding multiple items to a list in Python.

And that’s it for this article.

Heave ho!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *