w3resource

Python: Create a multidimensional list with zeros

Python List: Exercise - 85 with Solution

Write a Python program to create a multidimensional list (lists of lists) with zeros.

Visual Presentation:

Python List: Create a multidimensional list with zeros.

Sample Solution:

Python Code:

# Create an empty multidimensional list 'nums'
nums = []

# Loop over a range of 3 times to create 3 sublists
for i in range(3):
    # Append an empty sublist to 'nums' for each iteration
    nums.append([])
    
    # Loop over a range of 2 times to fill each sublist with 2 zeros
    for j in range(2):
        # Append a zero to the current sublist for each iteration
        nums[i].append(0)

# Print a message indicating the resulting multidimensional list
print("Multidimensional list:")

# Print the 'nums' list containing the structure of zeros
print(nums)

Sample Output:

Multidimensional list:
[[0, 0], [0, 0], [0, 0]]

Flowchart:

Flowchart: Create a multidimensional list with zeros.

Python Code Editor:

Previous: Write a Python program to round the numbers of a given list, print the minimum and maximum numbers and multiply the numbers by 5. Print the unique numbers in ascending order separated by space.
Next: Write a Python program to create a 3X3 grid with numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.