Append in Python – How to Append to a List or an Array (2023)

/ #Python
Append in Python – How to Append to a List or an Array (1)
Dionysia Lemonaki
Append in Python – How to Append to a List or an Array (2)

In this article, you'll learn about the .append() method in Python. You'll also see how .append() differs from other methods used to add elements to lists.

Let's get started!

What are lists in Python? A definition for beginners

An array in programming is an ordered collection of items, and all items need to be of the same data type.

However, unlike other programming languages, arrays aren't a built-in data structure in Python. Instead of traditional arrays, Python uses lists.

Lists are essentially dynamic arrays and are one of the most common and powerful data structures in Python.

You can think of them as ordered containers. They store and organize similar kind of related data together.

The elements stored in a list can be of any data type.

There can be lists of integers (whole numbers), lists of floats (floating point numbers), lists of strings (text), and lists of any other built-in Python data type.

Although it is possible for lists to hold items of only the same data type, they are more flexible than traditional arrays. This means that there can be a variety of different data types inside the same list.

Lists have 0 or more items, meaning there can also be empty lists. Inside a list there can also be duplicate values.

Values are separated by a comma and enclosed in square brackets, [].

How to create lists in Python

To create a new list, first give the list a name. Then add the assignment operator(=) and a pair of opening and closing square brackets. Inside the brackets add the values you want the list to contain.

#create a new list of namesnames = ["Jimmy", "Timmy", "Kenny", "Lenny"]#print the list to the consoleprint(names)#output#['Jimmy', 'Timmy', 'Kenny', 'Lenny']

How lists are indexed in Python

Lists maintain an order for each item.

Each item in the collection has an its own index number, which you can use to access the item itself.

Indexes in Python (and every other modern programming language) start at 0 and increase for every item in the list.

For example, the list created earlier on had 4 values:

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]

The first value in the list, "Jimmy", has an index of 0.

(Video) Python NumPy | Append

The second value in the list, "Timmy", has an index of 1.

The third value in the list, "Kenny", has an index of 2.

The fourth value in the list, "Lenny", has an index of 3.

To access an element in the list by its index number, first write the name of the list, then in square brackets write the integer of the element's index.

For example, if you wanted to access the element that has an index of 2, you'd do:

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]print(names[2])#output#Kenny

Lists in Python are mutable

In Python, when objects are mutable, it means that their values can be changed once they've been created.

Lists are mutable objects, so you can update and change them after they have been created.

Lists are also dynamic, meaning they can grow and shrink throughout the life of a program.

Items can be removed from an existing list, and new items can be added to an existing list.

There are built-in methods for both adding and removing items from lists.

For example, to add items, there are the .append(), .insert() and .extend() methods.

To remove items, there are the .remove(), .pop() and .pop(index) methods.

What does the .append() method do?

The .append() method adds an additional element to the end of an already existing list.

The general syntax looks something like this:

list_name.append(item)

Let's break it down:

  • list_name is the name you've given the list.
  • .append() is the list method for adding an item to the end of list_name.
  • item is the specified individual item you want to add.

When using .append(), the original list gets modified. No new list gets created.

If you wanted to add an extra name to the list created from earlier on, you would do the following:

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]#add the name Dylan to the end of the listnames.append("Dylan")print(names)#output#['Jimmy', 'Timmy', 'Kenny', 'Lenny', 'Dylan']

What's the difference between the .append() and .insert() methods?

The difference between the two methods is that .append() adds an item to the end of a list, whereas .insert() inserts and item in a specified position in the list.

As you saw in the previous section, .append() will add the item you pass as the argument to the function always to the end of the list.

If you don't want to just add items to the end of a list, you can specify the position you want to add them with .insert().

(Video) Using Python's .append() to Build Lists

The general syntax looks like this:

list_name.insert(position,item)

Let's break it down:

  • list_name is the name of the list.
  • .insert() is the list method for inserting an item in a list.
  • position is the first argument to the method. It's always an integer - specifically it's the index number of the position where you want the new item to be placed.
  • item is the second argument to the method. Here you specify the new item you want to add to the list.

For example, say you had the following list of programming languages:

programming_languages = ["JavaScript", "Java", "C++"]print(programming_languages)#output#['JavaScript', 'Java', 'C++']

If you wanted to insert "Python" at the start of the list, as a new item to the list, you would use the .insert() method and specify the position as 0. (Remember that the first value in a list always has an index of 0.)

programming_languages = ["JavaScript", "Java", "C++"]programming_languages.insert(0, "Python")print(programming_languages)#output#['Python', 'JavaScript', 'Java', 'C++']

If you instead had wanted "JavaScript" to be the first item in the list, and then add "Python" as the new item, you would specify the position as 1:

programming_languages = ["JavaScript", "Java", "C++"]programming_languages.insert(1,"Python")print(programming_languages)#output#['JavaScript', 'Python', 'Java', 'C++']

The .insert() method gives you a bit more flexibility compared to the .append() method which only adds a new item to the end of the list.

What's the difference between the .append() and .extend() methods?

What if you want to add more than one item to a list at once, instead of adding them one at a time?

You can use the .append() method to add more than one item to the end of a list.

Say you have one list that contains only two programming languages:

programming_languages = ["JavaScript", "Java"]print(programming_languages)#output#['JavaScript', 'Java']

You then want to add two more languages, at the end of it.

In that case, you pass a list containing the two new values you want to add, as an argument to .append():

programming_languages = ["JavaScript", "Java"]#add two new items to the end of the listprogramming_languages.append(["Python","C++"])print(programming_languages)#output#['JavaScript', 'Java', ['Python', 'C++']]

If you take a closer look at the output from above, ['JavaScript', 'Java', ['Python', 'C++']], you'll see that a new list got added to the end of the already existing list.

So, .append() adds a list inside of a list.

Lists are objects, and when you use .append() to add another list into a list, the new items will be added as a single object (item).

Say you already had two lists, like so:

names = ["Jimmy", "Timmy"]more_names = ["Kenny", "Lenny"]

What if wanted to combine the contents of both lists into one, by adding the contents of more_names to names?

When the .append() method is used for this purpose, another list gets created inside of names:

names = ["Jimmy", "Timmy"]more_names = ["Kenny", "Lenny"]#add contents of more_names to namesnames.append(more_names)print(names)#output#['Jimmy', 'Timmy', ['Kenny', 'Lenny']]

So, .append() adds the new elements as another list, by appending the object to the end.

To actually concatenate (add) lists together, and combine all items from one list to another, you need to use the .extend() method.

The general syntax looks like this:

(Video) Python Tutorial - append vs extend methods

list_name.extend(iterable/other_list_name)

Let's break it down:

  • list_name is the name of one of the lists.
  • .extend() is the method for adding all contents of one list to another.
  • iterable can be any iterable such as another list, for example, another_list_name. In that case, another_list_name is a list that will be concatenated with list_name, and its contents will be added one by one to the end of list_name, as separate items.

So, taking the example from earlier on, when .append() is replaced with .extend(), the output will look like this:

names = ["Jimmy", "Timmy"]more_names = ["Kenny", "Lenny"]names.extend(more_names)print(names)#output#['Jimmy', 'Timmy', 'Kenny', 'Lenny']

When we used .extend(), the names list got extended and its length increased by 2.

The way .extend() works is that it takes a list (or other iterable) as an argument, iterates over each element, and then each element in the iterable gets added to the list.

There is another difference between .append() and .extend().

When you want to add a string, as seen earlier, .append() adds the whole, single item to the end of the list:

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]#add the name Dylan to the end of the listnames.append("Dylan")print(names)#output#['Jimmy', 'Timmy', 'Kenny', 'Lenny', 'Dylan']

If you used .extend() instead to add a string to the end of a list ,each character in the string would be added as an individual item to the list.

This is because strings are an iterable, and .extend() iterates over the iterable argument passed to it.

So, the example from above would look like this:

names = ["Jimmy", "Timmy", "Kenny", "Lenny"]#pass a string(iterable) to .extend()names.extend("Dylan")print(names)#output#['Jimmy', 'Timmy', 'Kenny', 'Lenny', 'D', 'y', 'l', 'a', 'n']

Conclusion

To sum up, the .append() method is used for adding an item to the end of an existing list, without creating a new list.

When it is used for adding a list to another list, it creates a list within a list.

If you want to learn more about Python, check out freeCodeCamp's Python Certification. You'll start learning in an interacitve and beginner-friendly way. You'll also build five projects at the end to put into practice what you learned.

Thanks for reading and happy coding!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

(Video) Python Append vs. Extend

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Append in Python – How to Append to a List or an Array (3)
Dionysia Lemonaki

Read more posts.

If this article was helpful, .

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTISEMENT

FAQs

Append in Python – How to Append to a List or an Array? ›

Python append() function enables us to add an element or an array to the end of another array. That is, the specified element gets appended to the end of the input array.

How do you append a list to an array in Python? ›

1. Use the append() method to add one or more elements to a copy of a NumPy array. Provide the original variable and add the new values to an iterable. The append() method adds a list to the NumPy array and overwrites the old variable with new values.

How do you append to a Python list? ›

A call to .append() will place new items in the available space. Lists are sequences that can hold different data types and Python objects, so you can use .append() to add any object to a given list. In this example, you first add an integer number, then a string, and finally a floating-point number.

How do you append an array? ›

How to append to an array in Java
  1. Create a new, larger array.
  2. Copy over the content of the original array.
  3. Insert the new element at the end.

How do you create an array and append in Python? ›

By using the numpy library we can easily create an array using the numpy. array() method. In the same way, we can also append an element to the array using the numpy. append() method.

Can you append to an array list? ›

Approach: ArrayLists can be joined in Java with the help of Collection. addAll() method. This method is called by the destination ArrayList and the other ArrayList is passed as the parameter to this method. This method appends the second ArrayList to the end of the first ArrayList.

How do you append to an array first in Python? ›

Let us see a few different methods by which we can append a value at the beginning of a Python list.
  1. Using Insert() Method.
  2. Using [ ] and + Operator.
  3. Using List Slicing.
  4. Using collections.deque.appendleft()
  5. using extend() method.
  6. using list() and itertools.chain() Functions.
  7. Using numpy.concatenate() Function.
Jul 11, 2023

What does append () do in Python? ›

append() method. The . append() method takes an object as an argument and adds it to the end of an existing list. For example, suppose you create a list and you want to add another number to it.

What is the best way to append in Python? ›

If there are a few strings, then you can use any method to append them. From a readability perspective, using the += operator seems better for a few strings. However, if you have to append a lot of strings, then you should use the join() function. One can learn about more Python concepts here.

Can you append a string to a list Python? ›

By using append() you can add a string at the end of the list in Python. This method takes the string as an argument and adds the string to the existing list, this does not return any value.

How do I append to an array in pandas Python? ›

Pandas DataFrame append() Method

The append() method appends a DataFrame-like object at the end of the current DataFrame. The append() method returns a new DataFrame object, no changes are done with the original DataFrame.

What does append to array mean? ›

Append to Array in JavaScript is an operation in which the new array elements are added to the end or start of an existing array defined in JavaScript.

How do you append two elements to an array in Python? ›

Appending multiple items to a list using “extend” In Python, you can add multiple items to a list using the `extend()` method. The `extend()` method takes an iterable object (e.g. list, tuple, set) as an argument and adds each element of the iterable to the end of the list.

What is the difference between insert () and append () methods of a list? ›

append() adds an item to the end of a list, whereas . insert() inserts and item in a specified position in the list.

Can you append a list to a NumPy array? ›

In NumPy, to add elements or arrays, including rows and columns, to the end or beginning of an array ( ndarray ), use the np. append() function. Note that append() is not provided as a method of ndarray . This article explains the following contents.

How do I convert a list to an array? ›

Create a List object. Add elements to it. Create an empty array with size of the created ArrayList. Convert the list to an array using the toArray() method, bypassing the above-created array as an argument to it.

Videos

1. Python Numpy Append
(Python Maratón)
2. How to Use Lists in Python
(Programming with Mosh)
3. How to Append Data to a JSON File in Python?
(Finxter - Create Your Six-Figure Coding Business)
4. Python Programming: appending an array and summing its contents
(MrTsingo)
5. Python - How to Add / Append items to a list
(Learn Learn Scratch Tutorials)
6. Python - Adding/Appending Data to CSV Files
(The CS Classroom)
Top Articles
Latest Posts
Article information

Author: Errol Quitzon

Last Updated: 18/05/2023

Views: 5423

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.