0

How do I take input for T test cases with N values in a list.

Where T is the number of test cases and N is the length of inputs to be taken into a list

3
3
2 2 2
3
1 2 3
4
2 3 4 5

I want to get out the lists to work on them like:

[2, 2, 2]
[1, 2, 3]
[2, 3, 4, 5]

5 Answers 5

2

You can take inputs using Python List Comprehension. Input for T test cases with N values in a list. I am using Python 3.x.

T = int(input())  #Enter the No. of Testcases
input_list = [[j for j in input().split(' ')] for i in range(T)]
Sign up to request clarification or add additional context in comments.

1 Comment

you should note this requires someone to enter the input as "a b c d" for a row
0

You can simply use string's split method to separate elements by ' ' (space) and map function to convert string input values to integers.

for _ in range(int(input())):
    input()    # gets length of list, but doesn't store it
    input_list = list(map(int, input().split()))
    print(input_list)

1 Comment

how do they get out of the loop? why take the input and not do anything with the value?
0

Please note that this script does not do any validation on the input and the length of the test case (i.e. l) is not really required.

T = int(input())

testCases = []
for i in range(T):
    l = int(input())
    testCase = [int(x) for x in  input().split(" ")]
    testCases.append(testCase)

print(testCases)

1 Comment

what's the point of input l when you never use it?
0

Python

kases = int(input())
for kase in range(kases):
    N = int(input())
    result = 1
    for i in range(1, N + 1):
        result = result * i
    print (result)

1 Comment

this does not answer the question asked or provide a useful solution. please consider deleting your answer.
-3
test_case=int(input())
while test_case!=0:
     n=int(input())
     a=list(map(int,input().split()))
     test_case-=1

1 Comment

this is just wrong, its an infinite loop or it never enters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.