List with defined size | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

6th Oct 2016, 1:29 PM
Fhatuwani Luvhengo
Fhatuwani Luvhengo - avatar
4 Answers
+ 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.
6th Oct 2016, 6:21 PM
Luke Armstrong
+ 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.
6th Oct 2016, 3:24 PM
Nikhil
Nikhil - avatar
+ 1
this line will do the work list1.insert(3,int(input()))
6th Oct 2016, 6:19 PM
chandra sekhar
0
If you want to have fixed sized arrays, check numpy's arrays
9th Oct 2016, 9:54 AM
Amaras A
Amaras A - avatar