Python list of list initialize

The list is the most essential data structure in python. In Python, we add every type of element to a list. The list can be initialized by utilizing square brackets. Today we explain the importance of a 2D or two-dimensional list. We use different techniques to create 2D lists. A 2D list contains lists in a list or nested lists as its values. In python, list Initializing makes a new filtered list with initial elements. You should know the differences between these methods because they create problems in the program that can be very complex to handle.

Example 1:

In this example, we use the append[] method to initialize a list which creates a nested list with the specified values. The method to initialize a list is very simple and easy. To implement the initializing process, we use Spyder Compiler in Windows 10. Head over to the Spyder IDE and create a new file to write your 2D array code.

We have created two lists in this program and specified their names Set1 and Set2. Then we have used the append[] function to add Set1 into Set2 and then specified the list values.

Set1 = [0, 0]

Set2 = [0, 0]

Set1.append[Set2]

Print[Set1]

Once done! save your code file, specify its name with the .py extension. In our illustration, we used Initialize2Dlist to save our code file.

Now run your code file by hitting the F5 key from the keyboard and check how the append[] method works.

Example 2:

In our second illustration, we have used the loop technique to initialize a 2D Python list. Firstly, we have stated the list dimensions, and after that, we have initialized our list. We have used the range[] function, which accepts an integer value and returns a traversal object. For further code implementation, we use the same code Initialize2Dlist file.

List1, List2 = [2, 3]

Res = [[ 0 for I in range[List1]] for j in range [List2]]

Print[res]

Save your code file, run the program and check the range[] function to initialize the 2D python list.

Example 3:

In our third illustration, we use the comprehension method to initialize the python list with default elements. This is the simple Pythonic method to initialize a list. It allows us to make a list with the help of traversal objects. To process further, head over to the source code file and write your program code to illustrate how it works. In this program, we have stated and initialized two variables. After this, we have used a for loop, which takes two variables as an argument. Then we used a print function to show the output on the screen.

List1_row = 2

List2_columns = 2

Res = [[ 0 for x in range[List2_columns]] for i in range [List1_row]]

Print[res]

In our illustration, use the same code file Initialize2Dlist. Save and run the program and check the output on the screen.

Example 4:

In Python, itertools is a resourceful and efficient tool used individually or blended with other functions. In the comprehension method, the approach uses the repeat[] function. In this program, we have used an import module and then declared and initialized a variable. After this, we have used another variable and fed a list.repeat[] function, which takes two values. Then we used a print function to show the output on the screen. We implement all these in the Spyder compiler source code file just like we did earlier.

From itertools import repeat

Var = 2

Res = list[repeat ][0], Var]]

Print [res]

Again, save the code file and tap F5 to compile and run the code.

Example 5:

In our fifth illustration, we use the NumPy.full[] method to initialize a python list. This method is not fast as compared to the comprehension method. This function creates an array and uses the tolist[] function, which alters a 2D array to a Python List. To implement the program code, use Spyder compiler source code file just like we did earlier.

Import numpy

Num1_columns = 2

Num2_rows = 3

Result = numpy.full[[Num_columns, Num2_rows], 0].tolist[]

Print[result]

Save and run the code file and check the output on the screen.

Conclusion:

Python two-dimensional list has its pros and cons. The usage of a 2D list depends on the condition of the Python code. I hope you will understand the concept of a 2D list in Python by using all five illustrations mentioned in this tutorial.

Video liên quan

Chủ Đề