filmov
tv
flattening a nested list to single list using Python list comprehensions |Jupyter Notebook|

Показать описание
#explanation :
#Let's break down the code step by step:
nested_list = [[1, 2, 3, 4, 5], [8, 9, 11], [12, 45, 67]]
This is a list of lists, called a nested list. Each list inside the main list contains numbers.
flat_list = [num for sublist in nested_list for num in sublist]
This line creates a new list called flat_list using a method called list comprehension.
Here's what's happening:
for sublist in nested_list: This part loops through each sublist inside the nested_list.
for num in sublist: For each sublist, it loops through the individual numbers inside it.
num: Each number found in the sublists is added to the new flat_list.
So, the inner for num in sublist is nested inside the outer loop for sublist in nested_list. It extracts each number from the nested lists and puts it into the single flat_list.
print("so the list is", flat_list) #displayes the output
In a simpler way :
The nested_list contains sublists. The list comprehension extracts each number from all the sublists and creates a new single, flat list flat_list.
#education #learning #school #motivation #students #love #study #student #science #knowledge #teacher #children #college #india #covid #kids #university #learn #bhfyp #business #teaching #success #instagood #community #teachers #instagram #technology #inspiration #english #art
#science #learning #students #studentlife #knowledge #educational #study #viralvideo #teaching
#youtube #india #learnsmartly #datascience #python #photography
#Let's break down the code step by step:
nested_list = [[1, 2, 3, 4, 5], [8, 9, 11], [12, 45, 67]]
This is a list of lists, called a nested list. Each list inside the main list contains numbers.
flat_list = [num for sublist in nested_list for num in sublist]
This line creates a new list called flat_list using a method called list comprehension.
Here's what's happening:
for sublist in nested_list: This part loops through each sublist inside the nested_list.
for num in sublist: For each sublist, it loops through the individual numbers inside it.
num: Each number found in the sublists is added to the new flat_list.
So, the inner for num in sublist is nested inside the outer loop for sublist in nested_list. It extracts each number from the nested lists and puts it into the single flat_list.
print("so the list is", flat_list) #displayes the output
In a simpler way :
The nested_list contains sublists. The list comprehension extracts each number from all the sublists and creates a new single, flat list flat_list.
#education #learning #school #motivation #students #love #study #student #science #knowledge #teacher #children #college #india #covid #kids #university #learn #bhfyp #business #teaching #success #instagood #community #teachers #instagram #technology #inspiration #english #art
#science #learning #students #studentlife #knowledge #educational #study #viralvideo #teaching
#youtube #india #learnsmartly #datascience #python #photography