Append to 2D List in Python (3 Examples)

 

Hi! This tutorial will show you how to add a new element to a 2D list in the Python programming language.

Here is a quick overview:

Let’s get right into the Python code!

 

Create Demo 2D List

Here, we will create the demo 2D Python list of integers that we will attach a new element to. Therefore, in your preferred Python IDE, run the line of code below:

my_2Dlist = [[1, 2], [3, 4], [5, 6]]
 
print(my_2Dlist)
 
# [[1, 2], [3, 4], [5, 6]]
 
 
print(type(my_2Dlist))
 
# <class 'list'>

With the example 2D list created, let us now see examples of including a new element in the list.
 

Example 1: Add New Element to 2D List Using append() Method

In this first example, we will use the append() method to add a new element to the 2D list:

new_elem = [7, 8]
 
my_2Dlist.append(new_elem)
 
print(my_2Dlist)
 
 
# [[1, 2], [3, 4], [5, 6], [7, 8]]

In the above example, we added the new element to the 2D list by chaining the append() method containing the new element. Printing out the 2D list shows that my_2Dlist is now updated.

 

Example 2: Add New Element to 2D List Using extend() Method

In this next example, we will use the extend() method to attach a new element to the 2D list:

new_elem = [7, 8]
 
my_2Dlist.extend([new_elem])
 
print(my_2Dlist)
 
# [[1, 2], [3, 4], [5, 6], [7, 8]]

Much like the append() method in the previous example, the extend() method adds the new element to the 2D list. The only difference you might notice is that we wrapped the new element inside a pair of square brackets [ ] while including it in the 2D list. The extend() method uses only the values in the new element to populate the list by default; hence, we needed to wrap the new element inside square brackets to obtain a regular 2D structure.

 

Example 3: Add New Element to 2D List Using Plus Operator

In this final example, we will use the plus operator to append a new element to the 2D list:

my_2Dlist += [new_elem]
 
print(my_2Dlist) 
 
# [[1, 2], [3, 4], [5, 6], [7, 8]]

In the above example, we used the + operator to add the new element to the 2D list. The code above is a concise way of writing my_2Dlist = my_2Dlist + [new_elem]. Once again, we wrapped the new element inside a pair of square brackets to keep the 2D structure of my_2Dlist.

With that, we have demonstrated how to append a new element to a 2D list in Python. I hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to append a new element to a 2D list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to append a new element to a 2D list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to append a new element to a 2D list in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top