How do I take multiple inputs in python inside a for loop in a single line?

I am stuck in a problem as I am unable to take multiple inputs in a for loop in single line. I read online tutorials and watched few videos but couldn’t find any relevant information. Will appreciate any suggestion or tip. Thanks!

Can you please elaborate ,like what’s the exact issue??

1 Like

x=list(map(int,input().split()))
i think this the thing u want maybe…

1 Like

for i in range(n):
x = int(input())
For example In the above for loop the input will be in a new line every time. I want to take that input in the same line seperated by a space.
x = list(map(int,input().split())) works fine for taking multiple inputs outside loop but doesn’t work inside the loop.

This doesn’t work inside for loop though

arr=[]
n=int(input())
for i in range (0,n):
x = list(map(int,input().split()))
arr.append(x)
print(arr)

this would work

1 Like

I am a newbie on codechef and cp. Can you please explain what this means?

Thank u so much! Appreciate it

I can explain you how to read multiple inputs in Python.
let’s say you have T Test cases. In each test case, you are given two lines of Input. First line being N, the size of array and second line being N space separated Integers.
You are supposed to find the sum of array for each test case. The solution would be like this.

t = int(input())
for i in range(t):
    n = int(input()) # Reading N
    arr = list(map(int,input().split())) # Read Space separated integers
    ans = sum(arr)
    print(ans)

If in case you are getting TLE, which expands to Time limit Exceeded, then you should use Fast IO.
In Python, Fast IO is implemented using stdin and stdout Classes defined in sys module.
Example:

from sys import stdin, stdout
def input():
    return stdin.readline().strip()
def print(*args,sep = " ", end = "\n"):
    out = sep.join(str(i) for i in args) + end
    stdout.write(out)

def main():
    t = int(input())
    for i in range(t):
        n = int(input())
        arr = list(map(int,input().split()))
        print(sum(arr))
main()
5 Likes

This community is so nice! Thank you so much. I really appreciate it :slight_smile:

1 Like

I do have the same problem …
Is there anybody who knows to do the same in C?

Yes, it’s the same way to do in C.
Let’s say you have T Test cases. In each test case, you are given two lines of Input. First line being N , the size of array and second line being N space separated Integers .
You are supposed to find the sum of array for each test case. The solution would be like this.

#include <stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    for(;t>0;t--)
    {
        int n;
        scanf("%d",&n);
        int a[10001]; // The Integer inside [] must be
                      // greater than or equal to max(N)
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        int sum = 0;
        for(int i=0;i<n;i++)
            sum += a[i];
        printf("%d\n",sum);
    }
    return 0;
}

And basically, C’s IO is the fastest. Which means, scanf and printf statements are enough ( No need to optimise it further).
Most of the time (99%), this input and output methods will execute with in time limits.
If you get TLE, you must optimise your code (no need to optimise IO).