15 Respostas
+ 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...)
+ 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.
+ 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?
+ 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.
+ 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.
+ 2
The output is [ ], no?
+ 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.
0
Your code have many syntax error
You have declare array like
arr2 = [4]*n;
0
AbdulWahab brother thats why I'm asking how to declare a 2D Array in Python?
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?
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?
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!
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???😅
0
If you want to create multi-dimentional arrays in python, the best is to use NumPy library.