+ 1

n=int(input("Enter a number :") arr2[n][4]=[ ] why is this array declaration not working in Python?????

I want to declare a 2D array and fill it later on in python Please help!

14th Sep 2021, 10:08 AM
Farzam Baig
15 odpowiedzi
+ 6
Can you please put your code in a playground script and link it here? (It gets unreadable when one just pastes it into the heading...)
14th Sep 2021, 10:33 AM
Lisa
Lisa - avatar
+ 5
[None] * 4 (y) creates a list that contains 4 time None. This is repeated 3 times (x) by using a loop. _ is just a placeholder to iterate through the range. You could name is i or something different, that's up to you.
14th Sep 2021, 4:41 PM
Lisa
Lisa - avatar
+ 4
Farzam Baig , please give a brief description about how do you want to fill the inner lists. do they all have the same number of elements?
14th Sep 2021, 6:12 PM
Lothar
Lothar - avatar
+ 3
Farzam Baig I'm just asking you not to put code into the question heading – but rather in a script. Please check Tibor Santa 's code and numpy arrays.
14th Sep 2021, 1:11 PM
Lisa
Lisa - avatar
+ 2
You can do something like this to create a list of lists, with a specified size, where all elements are None. x, y = 3, 4 list2d = [[None] * y for _ in range(x)] print(list2d) https://code.sololearn.com/cNF8eDxv702A/?ref=app In Python we usually use lists, rather than arrays, unless performance is critically important (but then it might be better to pick a different language...) Numpy library offers additional ways to define this kind of matrix structure easily.
14th Sep 2021, 10:44 AM
Tibor Santa
Tibor Santa - avatar
+ 2
The output is [ ], no?
4th Oct 2021, 5:26 PM
CGO!
CGO! - avatar
+ 1
Farzam Baig An empty list has NO elements. In Python you cannot declare a List of 10 elements without saying what those values should be. empty = [] Then you can add each element with append() or insert() methods.
15th Sep 2021, 4:00 AM
Tibor Santa
Tibor Santa - avatar
0
Your code have many syntax error You have declare array like arr2 = [4]*n;
14th Sep 2021, 11:08 AM
AbdulWahab
AbdulWahab - avatar
0
AbdulWahab brother thats why I'm asking how to declare a 2D Array in Python?
14th Sep 2021, 12:33 PM
Farzam Baig
0
Lisa im just asking how to declare a 2D array in Python?
14th Sep 2021, 12:33 PM
Farzam Baig
0
Lisa please explain what is this line doing and how it's working list2d = [[None] * y for _ in range(x)] What is it doing?
14th Sep 2021, 4:18 PM
Farzam Baig
0
Tibor Santa please explain what is this line doing and how it's working list2d = [[None] * y for _ in range(x)] Why does [None]*y mean and why is "_" written there instead of a variable in for loop? What is it doing?
14th Sep 2021, 4:20 PM
Farzam Baig
0
Lothar anyways that's solved Now would you please be more kinder and tell me how to declare an empty List that can be filled later on???? Yes 1D not List of Lists Just a Simple empty lists with number of subscriptions entered by the user!
15th Sep 2021, 3:34 AM
Farzam Baig
0
Lisa you're so kind thanks🙏🙏🙏🙏🙏❤️❤️❤️🔥 Wait is it a coincidence or is there a reason that every time a post a question ,majority of the ppl answering are Germans???😅
15th Sep 2021, 3:36 AM
Farzam Baig
0
If you want to create multi-dimentional arrays in python, the best is to use NumPy library.
15th Sep 2021, 2:46 PM
Muhammad Bilal
Muhammad Bilal - avatar