In python can list be created in runtime. If yes means please explain me the code. Thanking u <3 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

In python can list be created in runtime. If yes means please explain me the code. Thanking u <3

In C, C++ one can create an array in runtime with size N using looping. Is that possible in python using list or which concept can be used to achieve it. :) Thanking u in advance 😊

23rd Jun 2017, 5:17 AM
S.Naveen Kumar
S.Naveen Kumar - avatar
9 Answers
+ 2
That was an example how to create a list of numbers using input by space. Map is function that gives you instructions applying some function to each item. So map receives two parameters: some function and list of items. In that example input().split() gives you a list of elements separated by space. But those elements aren't numbers. And "int" in map converts all that elements to integers. Finally you have a list of numbers.
23rd Jun 2017, 7:48 AM
Александр Громозонов
Александр Громозонов - avatar
+ 11
# That what you mean? l = list() for i in range(10): l.append(i/2) print(l) # or this: l = [i/2 for i in range(10)] print(l)
23rd Jun 2017, 5:30 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 9
l = [input("Gimme " + str(i) + ": ") for i in range(10)]
23rd Jun 2017, 6:22 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 4
For example: list l of numbers from 0 to determined n: n=int(input()) l=[i for i in range(n+1)] Other way: for i in range(n+1): l.append(i) You can use other methods to modify your list as you want.
23rd Jun 2017, 5:36 AM
Александр Громозонов
Александр Громозонов - avatar
+ 4
In this case you can use map with list comprehension. l=list(map(int, input().split())) print(l)
23rd Jun 2017, 6:22 AM
Александр Громозонов
Александр Громозонов - avatar
+ 1
In this case you can use map with list comprehension. l=list(map(int, input().split())) print(l) Sir can you explain it more clearly .
23rd Jun 2017, 7:16 AM
S.Naveen Kumar
S.Naveen Kumar - avatar
+ 1
Sir thanka a lot ☺. Now I got it. Time to play with it...
23rd Jun 2017, 8:08 AM
S.Naveen Kumar
S.Naveen Kumar - avatar
0
Thank you frnds but this loops assigns index values to list l, but I need to get input in runtime. So can u help me with it's solution. ☺
23rd Jun 2017, 6:14 AM
S.Naveen Kumar
S.Naveen Kumar - avatar
0
Frnds i have found solution in another logic : N = int(input("Enter Size of Array:")) l = list() print("Enter The Elements:") for i in range(N): ip = int(input()) l.append(ip) print("Elements in list:",l) sum_number = 0 for i in range(N): sum_number = int( l[i] ) +sum_number print("Sum of Array",sum_number) This program gets input from user and then sums all elements in the list . Thank you guys :)
23rd Jun 2017, 7:13 AM
S.Naveen Kumar
S.Naveen Kumar - avatar