Attributeerror: 'Tuple' Object Has No Attribute 'Split' with Result of Input()?

(This is Homework) Here is what I have:

L1 = list(map(int, input().split(",")))

I am running into

  File "lab3.py", line 23, in <module>
    L1 = list(map(int, input().split(",")))
AttributeError: 'tuple' object has no attribute 'split'

what is causing this error?

I am using 1, 2, 3, 4 as input

12

1 Answer

You need to use raw_input instead of input

raw_input().split(",")

In Python 2, the input() function will try to eval whatever the user enters, the equivalent of eval(raw_input()). When you input a comma separated list of values, it is evaluated as a tuple. Your code then calls split on that tuple:

>>> input().split(',')
1,2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'split'

If you want to se that it's actually a tuple:

>>> v = input()
1,3,9
>>> v[0]
1
>>> v[1]
3
>>> v[2]
9
>>> v
(1, 3, 9)

Finally, rather than list and map you'd be better off with a list comprehension

L1 = [int(i) for i in raw_input().split(',')]
0

Your Answer

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

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest