Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,239 questions

56,142 answers

573 users

How to create and set values to a 3d list in Python

1 Answer

0 votes
def initialize_list(lst3d, x, y, z):
    for i in range(x):
        for j in range(y):
            for k in range(z):
                lst3d[i][j][k] = i + j + k  # Initialization

def print_list(lst3d, x, y, z):
    for i in range(x):
        for j in range(y):
            for k in range(z):
                print(lst3d[i][j][k], end=" ")
            print()


x, y, z = 2, 3, 4

# Create a 3D list dynamically
lst3d = [[[0 for _ in range(z)] for _ in range(y)] for _ in range(x)]

initialize_list(lst3d, x, y, z)
print_list(lst3d, x, y, z)



'''
run:

0 1 2 3 
1 2 3 4 
2 3 4 5 
1 2 3 4 
2 3 4 5 
3 4 5 6 

'''

 



answered Apr 22, 2025 by avibootz
...