Master 2D Lists in Python with W3Schools Quick Guide

Logan Jul 13, 2026

Python, a powerful and versatile programming language, provides a rich ecosystem for various data manipulation tasks. One of the essential data structures in Python is the list, which can be one-dimensional (1D) or two-dimensional (2D). Today, we're going to delve into the world of 2D lists in Python, using the interactive and beginner-friendly online resource, w3schools.

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

Before we embark on this journey, let's ensure you have a solid foundation in 1D lists. A 1D list in Python is a mutable, ordered collection of items separated by commas and enclosed in square brackets. If you're already comfortable with 1D lists, then you're ready to explore their 2D counterparts, which are perfect for representing matrices and tables.

Python Lists – Master the Most Used Data Structure
Python Lists – Master the Most Used Data Structure

2D Lists in Python: Basics from w3schools

w3schools offers a comprehensive and user-friendly tutorial on 2D lists, guiding beginners through the process of creating, accessing, and manipulating these lists. Let's begin by understanding what exactly a 2D list is and how to create one.

Python list methods reference chart
Python list methods reference chart

A 2D list is a list that contains other lists. The outer list is called a table, and the inner lists are called rows. Each element in a row is called a column. Let's create a simple 2D list (table) with three rows and three columns ([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).

Creating a 2D List

a poster with the words, what is list slicing in python? and how to use it
a poster with the words, what is list slicing in python? and how to use it

To form a 2D list in Python, enclose the 1D lists (rows) inside an outer 1D list (table), separated by commas. For example:

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

The output of this code will be the 2D list we described earlier. Keep practicing to get comfortable with creating 2D lists.

Accessing Elements in a 2D List

python variables
python variables

Accessing elements in a 2D list requires indexing both the row and the column. Indexing in Python begins at 0, so to access the element at the first row and second column, you would use table[0][1], which returns 2. To print the entire second row, you would use table[1], which would output [4, 5, 6].

Let's practice accessing elements from our 2D list example:

print(table[0][1])  # Outputs: 2
print(table[1])    # Outputs: [4, 5, 6]

Manipulating 2D Lists: A Deep Dive

Master Python List Methods 🐍 | Beginner-Friendly Cheat Sheet
Master Python List Methods 🐍 | Beginner-Friendly Cheat Sheet

Once you're comfortable creating and accessing 2D lists, it's time to explore how to modify and add to them. w3schools provides invaluable insights into these manipulations, helping you become proficient in altering 2D lists as per your requirements.

Let's consider a 2D list called matrix:

a black background with text that reads python lists method, and an image of a computer screen
a black background with text that reads python lists method, and an image of a computer screen
Python list methods every developer should know.
Python list methods every developer should know.
💜 List vs Tuple in Python 🐍 Confused between Lists and Tuples?
💜 List vs Tuple in Python 🐍 Confused between Lists and Tuples?
Python list challenges cheat-sheet
Python list challenges cheat-sheet
Python List Methods
Python List Methods
the cover of collection data types in python, with an image of numbers and symbols
the cover of collection data types in python, with an image of numbers and symbols
Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners
"6 Python List Methods Every Beginner Should Know"
"6 Python List Methods Every Beginner Should Know"
Python
Python
Python list exercises guide
Python list exercises guide
Reverse a List in Python (Easy Method for Beginners)
Reverse a List in Python (Easy Method for Beginners)
Python Lists in Python Programming
Python Lists in Python Programming
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
an image of some cats with the words python list in front of them and below it
an image of some cats with the words python list in front of them and below it
Python List Methods
Python List Methods
🐍 Python List Methods Cheat Sheet ✨
🐍 Python List Methods Cheat Sheet ✨
Python List Comprehension Cheat Sheet 📌
Python List Comprehension Cheat Sheet 📌
Python Lists infographic for beginners
Python Lists infographic for beginners
Python Syntax Data Collections
Python Syntax Data Collections
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)

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

Changing Existing Elements

To change an existing element in a 2D list, provide both its row and column indices in square brackets. For instance, to change the value of the third element in the first row from 3 to 10, you would use:

matrix[0][2] = 10

Now, matrix[0] becomes [1, 2, 10]. Let's verify this by printing the first row:

print(matrix[0])  # Outputs: [1, 2, 10]

Adding Rows and Columns

You can also add new rows or columns to an existing 2D list. To add a new row, simply append it to the end of the 2D list. To add a new column, you'll need to update each row. Let's add a new row at the end of our matrix and add a new column storing the product of the corresponding elements from the first and second columns:

# Add a new row
matrix.append([1, 2, 3, 4])  # Now matrix has 4 rows

# Add a new column by calculating the product of elements from the first and second columns
for i in range(len(matrix)):
    matrix[i].append(matrix[i][0] * matrix[i][1])

print(matrix)

This will output the following 2D list:

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

Exploring w3schools' comprehensive guide on 2D lists and practicing the various creation, access, and manipulation techniques will help you build a strong foundation in working with 2D lists in Python.

To take your understanding to the next level, consider exploring more advanced topics like nested loops, iterating over 2D lists, and performing operations on 2D lists, such as transposing and traversing. Keep practicing and coding, and remember to consult w3schools whenever you need a refresher on Python concepts.

Happy coding! We hope you've found our exploration of 2D lists in Python using w3schools insightful and engaging. As you continue your Python journey, we encourage you to experiment with different data structures and libraries to expand your skills and build impressive projects.