Appending to 2D List in Python Last Updated : 17 Dec, 2024 Comments Improve Suggest changes Like Article Like Report A 2D list in Python is a list of lists where each sublist can hold elements. Appending to a 2D list involves adding either a new sublist or elements to an existing sublist. The simplest way to append a new sublist to a 2D list is by using the append() method. Python a = [[1, 2], [3, 4]] # Append a new sublist a.append([5, 6]) print(a) Output[[1, 2], [3, 4], [5, 6]] Explanation:append() adds a new sublist [5, 6] as a single element to the 2D list.The original structure of the 2D list remains unchanged.Let's explore some other methods to append to 2D List in PythonTable of ContentAppending to an Existing SublistUsing += Using List ComprehensionUsing extend() to Flatten a 2D ListAppending to an Existing SublistIf we want to append an element to an existing sublist in the 2D list, you can directly append it to that sublist. Python a = [[1, 2], [3, 4]] # Append an element to the second sublist a[1].append(5) print(a) Output[[1, 2], [3, 4, 5]] Explanation:Access the desired sublist using indexing (a[1]) then apply append() to add an element.This modifies the contents of the sublist while keeping the 2D structure intact.Using +=+= operator allows you to extend a sublist with multiple elements in a concise manner. Python a = [[1, 2], [3, 4]] # Extend the first sublist with multiple elements a[0] += [5, 6] print(a) Output[[1, 2, 5, 6], [3, 4]] Explanation:+= modifies the sublist in place by appending elements from another list.This is equivalent to using extend().Using List ComprehensionIf we want to append multiple sublists dynamically list comprehension is a concise option. Python a = [[1, 2], [3, 4]] # Add multiple sublists a += [[5, 6], [7, 8]] print(a) Output[[1, 2], [3, 4], [5, 6], [7, 8]] Explanation:List comprehension generates sublists dynamically and appends them to the original list.This method is concise and maintains readability for batch operations.Using extend() to Flatten a 2D ListIf we wish to merge another 2D list into the current one extend() is an efficient choice. Python a = [[1, 2], [3, 4]] # Extend the 2D list with another 2D list a.extend([[5, 6], [7, 8]]) print(a) Output[[1, 2], [3, 4], [5, 6], [7, 8]] Explanation:extend() adds elements of another iterable to the list without nesting.This method works well for combining two 2D lists. Comment More infoAdvertise with us Next Article Appending to 2D List in Python V vishuvaishnav3001 Follow Improve Article Tags : Python python-list Practice Tags : pythonpython-list Similar Reads Extending a list in Python In Python, a list is one of the most widely used data structures for storing multiple items in a single variable. Often, we need to extend a list by adding one or more elements, either from another list or other iterable objects. Python provides several ways to achieve this. In this article, we will 2 min read Python - Creating a 3D List In Python, 3D list represents a three dimensional data structure where we can organize elements in three axes (rows, columns, and depth). Letâs explore various ways to create a 3D list in Python.Using Nested List ComprehensionsNested list comprehensions are a concise way to create 3D lists by iterat 2 min read range() to a list in Python In Python, the range() function is used to generate a sequence of numbers. However, it produces a range object, which is an iterable but not a list. If we need to manipulate or access the numbers as a list, we must explicitly convert the range object into a list. For example, given range(1, 5), we m 2 min read How to Split a File into a List in Python In this article, we are going to see how to Split a File into a List in Python. When we want each line of the file to be listed at consecutive positions where each line becomes an element in the file, the splitlines() or rstrip() method is used to split a file into a list. Let's see a few examples 5 min read Multi-dimensional lists in Python There can be more than one additional dimension to lists in Python. Keeping in mind that a list can hold other lists, that basic principle can be applied over and over. Multi-dimensional lists are the lists within lists. Usually, a dictionary will be the better choice rather than a multi-dimensional 3 min read Like