I was solving a problem on hackerrank and encountered a problem reading inputs.
The input format is:
First line: A number n, which tells the no. of lines I have to read.
n lines: Two space separated values, e.g.:
1 5
10 3
3 4
I want to read the space separated values in two lists. So list 'a' should be [1,10,3] and list 'b' should be [5,3,4].
Here is my code:
dist = []
ltr = []
n = input()
for i in range(n):
ltr[i], dist[i] = map(int, raw_input().split(' '))
It gives me following error:
ltr[i], dist[i] = map(int, raw_input().split(' '))
IndexError: list assignment index out of range.
inputin Python 2, since it's insecure and allows a user to execute arbitrary code on your machine. If you want integers, convert the strings thatraw_inputreturns with theintfunction, e.g.:n = int(raw_input()). In Python 3raw_inputhas been renamed toinputand Python 2'sinputfunction doesn't exist anymore.