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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

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

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,941 questions

51,879 answers

573 users

How to declare 3D list in Python

3 Answers

0 votes
n = 3

lst3d = [[[0 for k in range(n)] for j in range(n)] for i in range(n)]

print(lst3d)

 
      
  
'''
run:
  
[[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]

'''

 



answered Apr 17, 2021 by avibootz
0 votes
n = 3

lst3d = [[[0] * n] * n] * n

print(lst3d)

 
      
  
'''
run:
  
[[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]

'''

 



answered Apr 17, 2021 by avibootz
0 votes
import numpy as np

i = 3
j = 3
k = 3

lst3d = np.zeros((i, j, k))

print(lst3d)


      
  
'''
run:
  
[[[0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]]

 [[0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]]

 [[0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]]]

'''

 



answered Apr 17, 2021 by avibootz

Related questions

1 answer 143 views
1 answer 155 views
2 answers 237 views
1 answer 141 views
141 views asked Oct 26, 2018 by avibootz
3 answers 270 views
1 answer 173 views
...