+ 1
List with defined size
In C++, we have arrays and you are supposed to define the size of the array (how may items it can hold) before using it. is that possible with lists in Python? I want to define an empty list that will be filled up with multiple user inputs.
4 Réponses
+ 2
It can be done as Yash says, but there is usually no need for that since python lists can always become bigger or smaller. you can declare an empty list and then append the user inputs to it to get as many list items as you need without explicitly saying which index to use.
in fact, the only advantage that i know of to filling a list with 0s before getting the inout is if you want to put each users input into a specific spot.
+ 1
Yes you can do it by putting 0s in list.
For example if you need a list of length of 5 write
list1 = [0]*5
it will automatically put 5 0s in list1. Now you can take inputs from user and put in it like,
list1[0] = input('Enter your first input : ')
list1[1] = input('Enter your second input :')
and so on.
+ 1
this line will do the work
list1.insert(3,int(input()))
0
If you want to have fixed sized arrays, check numpy's arrays