0
How can I fill list with "for" loop in python?
Like in c++ for(int i=0;i<=10;i++){ cin>>a[i];}
7 Antworten
+ 4
Almas The best Lists don't have predefined sizes, the size will change like it will get items.
+ 3
Are you talking about list comprehensions?
List comprehension examples:
[i for i in range(5)]
#[0, 1, 2, 3, 4]
[i * i for i in range(5)]
#[0, 1, 4, 9, 16]
[i for i in range(10) if i % 2 != 0]
#[1, 3, 5, 7, 9]
The for loop of C++ doesn't seem to have anything related to list "filling".
The list comprehensions do not correspond the C++ sample.
+ 2
Oops, I understood as cout, sorry.
+ 2
Here's the correspondings (The list a must have atleast 10 items):
for i in range(10):
a[i] = input()
i = 0
while i < 10:
a[i] = input()
i += 1
0
No
I mean that i should input elements in loop
0
Thx