Sure, here is a function to flatten a nested list in Python:
Python
def flatten(list1):
"""
Flattens a nested list.
Args:
list1: The nested list to flatten.
Returns:
A flattened list.
"""
flattened_list = []
for item in list1:
if isinstance(item, list):
flattened_list.extend(flatten(item))
else:
flattened_list.append(item)
return flattened_list
if __name__ == "__main__":
list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = flatten(list1)
print(flattened_list)
This function takes a nested list as input and returns a flattened list. The function works by first creating an empty list called
flattened_list. Then, it iterates through the original list and adds each item to the
flattened_list if the item is a list. If the item is not a list, the function simply appends it to the
flattened_list. Finally, the function returns the flattened_list.
To run the function, you can save it as a Python file and then run it from the command line. For example, if you save the function as
flatten.py, you can run it by typing the following command into the command line:
Code snippet
python flatten.py
This will print the flattened list to the console.
Here is an example of the output of the function:
Code snippet
$ python flatten.py
[1, 2, 3, 4, 5, 6, 7, 8, 9]
As you can see, the output of the function is a flattened list containing all the elements of the original nested list.
Here are some other ways to flatten a nested list in Python:
Using the itertools.chain() function.
Using the list comprehension syntax.
Using the numpy.ndarray.flatten() method.
The best way to flatten a nested list depends on the specific needs of your program.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is a function to flatten a nested list in Python:
Python
This function takes a nested list as input and returns a flattened list. The function works by first creating an empty list called
flattened_list. Then, it iterates through the original list and adds each item to theflattened_listif the item is a list. If the item is not a list, the function simply appends it to theflattened_list. Finally, the function returns theflattened_list.To run the function, you can save it as a Python file and then run it from the command line. For example, if you save the function as
flatten.py, you can run it by typing the following command into the command line:Code snippet
This will print the flattened list to the console.
Here is an example of the output of the function:
Code snippet
As you can see, the output of the function is a flattened list containing all the elements of the original nested list.
Here are some other ways to flatten a nested list in Python:
itertools.chain()function.list comprehensionsyntax.numpy.ndarray.flatten()method.The best way to flatten a nested list depends on the specific needs of your program.