✨ OpenCode | AI Coding Tools ✨
📖 An open-source terminal AI coding agent with support for over 75 AI models and IDE integrations.
🏷️ #Python
📖 An open-source terminal AI coding agent with support for over 75 AI models and IDE integrations.
🏷️ #Python
A bit of #Python basics. Day 8 - Flatten a nested list
I'll show you three (3) ways to flatten a two-dimensional list. The first method uses a
⚙️ Using a
For this method, we use a nested
⚙️ Using the itertools module:
The
You can see that the nested loop has been flattened.
⚙️ Using list comprehension
If you don't want to import
List comprehension is well suited for moderately nested lists. For deeply nested lists, it is not suitable, as the code becomes harder to read.
⚙️ Using a generator function
You can create a generator function that yields elements from the nested list, and then convert the generator into a list.
The generator method is suitable for flattening large or deeply nested lists. This is because generators are memory-efficient.
👉 https://t.iss.one/DataScience4
I'll show you three (3) ways to flatten a two-dimensional list. The first method uses a
for loop, the second uses the itertools module, and the third uses list comprehension.for loop:For this method, we use a nested
for loop. The outer loop iterates over the inner lists, and the inner loop accesses the elements in the inner lists.# In [19]:
list1 = [[1, 2, 3],[4, 5, 6]]
newlist = []
for list2 in list1:
for j in list2:
newlist.append(j)
print(newlist)[1, 2, 3, 4, 5, 6]The
itertools.chain.from_iterable() function from the itertools module can be used to flatten a nested list. This method may not be suitable for deeply nested lists.# In [20]:
import itertools
list1 = [[1, 2, 3],[4, 5, 6]]
flat_list = list(itertools.chain.from_iterable(list1))
print(flat_list)[1, 2, 3, 4, 5, 6]You can see that the nested loop has been flattened.
If you don't want to import
itertools or write a regular for loop, you can simply use list comprehension.# In [21]:
list1 = [[1, 2, 3], [4, 5, 6]]
flat_list = [i for j in list1 for i in j]
print(flat_list)[1, 2, 3, 4, 5, 6]List comprehension is well suited for moderately nested lists. For deeply nested lists, it is not suitable, as the code becomes harder to read.
You can create a generator function that yields elements from the nested list, and then convert the generator into a list.
# In [22]:
def flatten_generator(nested_list):
for sublist in nested_list:
for item in sublist:
yield item
list1 = [[1, 2, 3], [4, 5, 6]]
flat_list = list(flatten_generator(list1))
flat_list
Out[22]: [1, 2, 3, 4, 5, 6]The generator method is suitable for flattening large or deeply nested lists. This is because generators are memory-efficient.
Please open Telegram to view this post
VIEW IN TELEGRAM
Telegram
Code With Python
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Admin: @HusseinSheikho || @Hussein_Sheikho
❤6
✨ Kilo Code | AI Coding Tools ✨
📖 An open-source AI coding agent for VS Code, JetBrains, and the command line with support for over 500 AI models.
🏷️ #Python
📖 An open-source AI coding agent for VS Code, JetBrains, and the command line with support for over 500 AI models.
🏷️ #Python