Create Nested list using function | Python Essentials
Summary
TLDRIn this Python essentials video, Nikhil demonstrates how to create a nested list in Python. He starts by defining a function that generates a list of numbers from 0 to 9. Then, using a while loop, he initializes an empty list called 'nest_list' and uses the 'append' method to create a nested list structure with 10 lists, each containing numbers from 0 to 9. The video clarifies the difference between using 'extend' and 'append' for creating nested lists.
Takeaways
- π The video explains how to create a nested list using Python lists, loops, and functions.
- π§βπ« The speaker mentions that using arrays would make this easier, but the example focuses on using Python lists.
- π οΈ A user-defined function is used to generate a single list with values from 0 to 9.
- βοΈ The function has no arguments and initializes an empty list, then appends values in a range of 0 to 9 using a for loop.
- π The video suggests an alternative approach using a while loop to create a similar list.
- π The `extend()` function adds individual elements from the list to the main list, leading to a non-nested result.
- π To create a nested list, the speaker switches to the `append()` method, which adds each generated list as an element within the main list.
- π’ The final nested list has 10 sub-lists, each containing numbers 0 to 9, as intended.
- π‘ The video contrasts the usage of `extend()` and `append()` to show how they affect the list structure differently.
- β By using `append()`, the desired result of 10 lists nested within a larger list is achieved.
Q & A
What is the purpose of the video?
-The video aims to teach viewers how to create a nested list in Python using lists and functions.
What data structure does the video focus on, and why?
-The video focuses on lists because Python does not have arrays in the traditional sense, so nested lists are used to create multi-dimensional structures.
What is the first step in creating a nested list as explained in the video?
-The first step is to define a function that generates a single list containing the values 0 to 9.
How does the function 'listnum' generate the list?
-The function 'listnum' initializes an empty list, then uses a 'for' loop to iterate from 0 to 9, appending each value to the list, and finally returns the list.
Why does the function not take any arguments?
-The function does not require any arguments because it generates a predefined list of values (0 to 9), making input parameters unnecessary.
What mistake did the video highlight when trying to create a nested list using 'extend'?
-Using 'extend' caused the elements of each list to be added to a single list continuously, instead of creating a true nested list. 'Extend' merges elements, which prevents the formation of separate lists within the parent list.
What change is needed to correctly create a nested list?
-To correctly create a nested list, 'append' should be used instead of 'extend', which ensures that each list is added as an individual element to the main list.
What is the result when 'append' is used instead of 'extend'?
-When 'append' is used, the output is a nested list where each inner list (containing values 0 to 9) is a separate element, repeated 10 times.
How is the while loop used in creating the nested list?
-The while loop is used to repeatedly append the list generated by 'listnum' to the main nested list, iterating 10 times to create 10 inner lists.
What is the significance of adding an incrementer in the while loop?
-The incrementer ensures that the while loop progresses and eventually terminates, preventing an infinite loop from occurring.
Outlines
π¬ Introduction to Creating a Nested List in Python
The video begins with an introduction by Nikhil, welcoming viewers to learn about creating a nested list in Python. He explains that although using arrays would be easier when studying them later, the focus will be on lists for now. Nikhil hints at the use of 'for' and 'while' loops, and user-defined functions to achieve this goal. The objective is to generate ten lists, each containing values from 0 to 9, and nest them within a larger list.
π§ Defining a Function to Generate a Single List
Nikhil explains how to define a function that creates a list of numbers from 0 to 9. He walks through the steps, starting with defining an empty list, followed by a 'for' loop that appends values from 0 to 9 into the list. The function is simple and does not require any arguments. Nikhil emphasizes the use of a docstring to document the function, which generates and returns a list with values 0 to 9. After running the function, the expected outputβa list of numbers from 0 to 9βis displayed.
π Attempting a While Loop for Nested Lists
Nikhil shifts to using a 'while' loop to create a nested list. He initializes the value 'i' and discusses how the loop will run as long as 'i' is less than 10, incrementing by 1 each time. The final objective is to create a 'nest_list', which is an empty list that will store the nested lists. He suggests using the previously defined 'listnum' function to generate the inner lists and then discusses different methods of adding the lists into 'nest_list', such as the 'extend' method.
π« Correcting the Extend Method Mistake
Nikhil runs into an issue when using the 'extend' method to add lists. Instead of creating nested lists, the values from the second list get added directly after the first list. He explains that 'extend' simply adds elements to the existing list, which is not what was intended. To fix this, he suggests switching to the 'append' method, which correctly adds each list as a separate element within the 'nest_list', thus forming the desired 10x10 nested list. Nikhil concludes by showcasing the correct nested list output.
Mindmap
Keywords
π‘Nested list
π‘Function
π‘For loop
π‘While loop
π‘Append
π‘Extend
π‘Range
π‘List
π‘Empty list
π‘User-defined function
Highlights
Introduction to creating a nested list in Python using basic functionalities like for loops, while loops, and user-defined functions.
Importance of understanding arrays and how lists serve as a Python substitute when working within its constraints.
Explanation of how to generate a list with values from 0 to 9 using a simple Python function.
Defining a function without any arguments to return a list from 0 to 9.
Detailed breakdown of how to use a for loop to append values to the list.
Creating a single list of 10 values (from 0 to 9) through a function and understanding list generation in Python.
The role of the return statement in functions to return the newly generated list.
Using a while loop for list creation and ensuring loop control with an incrementer to avoid infinite loops.
Introduction to using the extend() function and its impact when incorrectly trying to create nested lists.
Understanding the difference between extend() and append() when working with lists.
Explanation of how extend() adds elements to the original list instead of creating a new nested list.
Switching to append() to correctly generate a nested list with 10 rows and 10 columns.
Using append() to create a list of lists where each element is itself a list.
Final result: A nested list with 10 lists, each containing values from 0 to 9.
Conclusion and validation of the code to ensure it generates the desired nested list structure.
Transcripts
[Music]
hello and welcome to another video on
python essentials
my name is nikhil so today we're going
to take a look at
how to create a nested list within the
constraints of python
all right
so going forward
when you'll be studying arrays
creating a nested list would be a lot
easier you'd probably just create an
array directly
but given that we have lists only to
deal with
and
certain functionalities like the
for and while loops
and let's also use the
user defined function
okay just to take a look at
how we can do that
so to begin with we are talking about
generally being able to see
one list 0 2 with value 0 to 9 that's
about 10 values in a single list
after that you have
another list separate these two lists
are separated by a comma
right and we'll proceed to create 10
such lists
and put them within
a larger list all right or the larger
pair of square brackets that's the
objective
so
let's begin with defining a function
all right a function which generates
a single list just one list which
contains the values
0 to 9.
let's call this list num
we
we already know what we're going to
generate we do not require any arguments
whatsoever
so this is a function without any
arguments
and uh
let's talk about defining an empty list
all right
following your empty list we say
for i in range
0 to 10
all right or we could just specify 10
that would suffice
so this is basically going to take us
from the values of 0 to
9.
all right
so for that range
we are simply going to
append the value of i
to the list
right so beginning with 0 will be
appending each value up to 9
and our list would be ready
with the 10 values
following that we just need to
return this list
okay so that's our simple function
let's have a dog string over here
just to specify
the function
returns a list with
values
0 to
9. all right
let's run this
so if we call the function
there we go we have a list with the
number zero to nine
all right
and uh
we could work with a while loop
right
as long as we initialize
the value
while i
less than 10 so that will take us from
beginning from i equal to 0 to i is
equal to 9
we have
well we will be required to
initialize another list which will be
our
final list
let's call it
nest list
and give it as an empty as an empty list
right
so at this point we are
we're going to make use of the
list now
all right as a function we are returning
the
value of
we're basically returning this list
all right so what we can do is simply
equate
a number or any variable
to
listnum
all right
and
we can
[Music]
call these elements or append these
elements let's take a look at extend all
right
just put a change
if we use extend
by a
okay
let's see what it does
so nest underscore list dot extend a and
we need to provide an incrementer of one
so that the while loop doesn't run
indefinitely
okay
so that's about it
let's see what we're getting
okay by running
nest list
okay here's what you have you have the
value 0 to 9
but what's happening is the next set of
values are continuing right after okay
so you could see it goes on
10 times but you are not quite getting
the nested list
this is because we used
extend
right we are extending it by a list
which means just adding these elements
to the original list we are not actually
talking about
adding another list altogether
so for that purpose we'll have to
stick with append
all right
so what's going to happen is we're going
to append each and every list
as an element itself
all right
now if we run the nest list
you can see you have about
10 rows and
10 columns but basically you're talking
about
this nested list 0 to 9 repeating
10 times
which is what
we were hoping to gain
all right
hope this is clear
you
5.0 / 5 (0 votes)