Skip to content

Python 2D array append

  • by

To append elements to a 2D array in Python, you can use the append() method available for Python lists. Here’s the syntax for appending elements to a 2D array:

array_2d.append([element1, element2, ...])
  • array_2d is the name of your 2D array (a list of lists).
  • element1, element2, and so on are the elements you want to append as a new row to the 2D array.

Python 2D array append example

Here’s an example that demonstrates how to append elements to a 2D array (a list of lists) in Python:

# Initialize a 2D array
array_2d = [[1, 2, 3], [4, 5, 6]]

# Append a new row to the 2D array
new_row = [7, 8, 9]

# Append elements to an existing row in the 2D array
existing_row_index = 1

# Print the modified 2D array
for row in array_2d:


Python 2D array append

We specify the index of the row (existing_row_index) and use the append() method to add elements directly to that row.

Finally, we iterate over the modified 2D array and print each row to verify the changes.

Do comment if you have any doubts or suggestions on this Array topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

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