1

I am trying to get space separated inputs. while the first method works completely fine, the second method throws an error saying:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

what is wrong with the second method?

Method 1:

x = [int(j) for j in input().split()]

Method 2:

x = [j for j in int(input().split())]
2
  • The error message is telling you exactly what's wrong. Commented Sep 21, 2018 at 11:31
  • Since you apply split to a str you get a list, which you feed to int. Hence the error. Commented Sep 21, 2018 at 11:32

1 Answer 1

2

Because you are using split() to a string which will return a list, then you are passing this list to int() that's why you are getting error. for changing datatype of list you need to use map() as below or first approach of your's.

Try Below code

x = [j for j in map(int,input().split())]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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