Click here to Skip to main content
15,946,342 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to print the every t,x integers as a list.
For example if input is
3 //n
2 4 //t,x
5 6
6 8
Then my output should be
[[2,4],[5,6],[6,8]]
If i print the below code i will get only the last t,x which is 6 8.
How can i get all the integers in List

What I have tried:

Python
n=int(input())
for i in range(n):
       t,x=map(int,input().split())
print(t,x)
Posted
Updated 18-Mar-22 23:26pm
v2

In Python, indentation is significant: all code with the same indentation is in the same code block. Once the indent gets smaller - even one character smaller - that code block ends.
So this:
Python
n=int(input())
for i in range(n):
       t,x=map(int,input().split())
print(t,x)
Has only one line of code inside the for loop:
Python
t,x=map(int,input().split())
That line will be executed n times, but
Python
print(t,x)
will be executed once. To "fix" this, you need to indent the whole block to the same level:
Python
n=int(input())
for i in range(n):
       t,x=map(int,input().split())
       print(t,x)
 
Share this answer
 
Comments
sanjay surya 19-Mar-22 2:57am    
Eventhough i indent i get only last line. How can i get the output as list
OriginalGriff 19-Mar-22 3:15am    
CHeck your indentation carefully: in Python tabs and spaces are not the same thing, so what looks like identical indentation might not be! (Python is a stupid language in this respect)
Try unindenting it completely, then indent each line in the block three spaces.

Also check that your editor is set to "use spaces instead of tabs" to make it really obvious.
You need to start with an empty list. Then for each pair of numbers create a new list and add it to the main one. Something like:
Python
n=int(input('please enter the count: '))
result = []
for i in range(n):
    t,x=map(int,input('please enter the next pair of numbers: ').split())
    temp = [t, x]
    result.append(temp)
print('The result is', result)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900