Python Forum
ValueError: invalid literal for int() with base 10: '[2,4,7,8,19]' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: ValueError: invalid literal for int() with base 10: '[2,4,7,8,19]' (/thread-4655.html)



ValueError: invalid literal for int() with base 10: '[2,4,7,8,19]' - rajeev1729 - Sep-01-2017

Hello friends,given program show given error. Please give me solution.
i want '[2,4,7,8,19]' as list of element but it is string.
ValueError: invalid literal for int() with base 10: '[2,4,7,8,19]'[/python]

def binarySearch (arr, first, last, element):
    if last < 1:
        return -1
    else: 
        mid = first+ (last-1)//2
        if arr[mid] == element:
            return mid
        elif arr[mid] >element:
            return binarySearch(arr, first, mid-1, element)
        else:
            return binarySearch(arr, mid+1, last, element)
 
arr=int(input("Enter list of element:"))
print(len(arr))
#Enter list of element:[2,5,7,9,10]
element=int(input("Enter list of element:"))
print(element)
#arr = [ 2, 3, 4, 10, 40 ]
#print(len(arr))
#element = 10
result = binarySearch(arr, 0, len(arr)-1, element)
if result != -1:
    print("Element is present at index %d" % result)
else:
    print("Element is not present in array")

Output:    
Enter list of element:[2,4,7,8,19]
Traceback (most recent call last):
  File "C:\Python 3.6\program\binary1.py", line 13, in <module>
    arr=int(input("Enter list of element:"))
ValueError: invalid literal for int() with base 10: '[2,4,7,8,19]'



RE: ValueError: invalid literal for int() with base 10: '[2,4,7,8,19]'[/python] - Larz60+ - Sep-01-2017

if broken down, your code is trying to convert a string with non numeric characters
>>> arr = input("Enter list of element:")
>>> arr
'2,4,7,8,19'
>>> int(arr)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '2,4,7,8,19'
>>> # correct as follows
...
>>> arr = arr.split(',')
>>> arr
['2', '4', '7', '8', '19']
>>>newarr =
>>>for element in arr:
...    newarr.append(int(element))
...
>>>print(newarr)
[2, 4, 7, 8, 19]
>>>print(newarr[3])
8
so i n a script, what you need to do is:

try:
    arr = input("Enter list of elements: ")
    print(arr)
    arr = arr.split(',')
    print(arr)
    newarr = []
    for element in arr:
        newarr.append(int(element))
    print(newarr)
except ValueError:
    print('Try again: Your input: {} contains non-numeric data'.format(arr))
Output:
Enter list of elements: 2,3,4,5,6 2,3,4,5,6 ['2', '3', '4', '5', '6'] [2, 3, 4, 5, 6]
Process finished with exit code 0
this can be simplified into a list comprehension:


RE: ValueError: invalid literal for int() with base 10: '[2,4,7,8,19]'[/python] - hbknjr - Sep-01-2017

As Larz60+ told, you can use .split(",") to split your array and then convert each element to int. Another way is to do it in one line.

(Sep-01-2017, 12:25 PM)Larz60+ Wrote: this can be simplified into a list comprehension:

>>> arr = [int(x) for x in input("Enter list of element:").split(',')]
Enter list of element:2,4,7,8,19
>>> print(arr)
[2, 4, 7, 8, 19]



RE: ValueError: invalid literal for int() with base 10: '[2,4,7,8,19]' - nilamo - Sep-15-2017

I also like simplifying things!

>>> arr = list(map(int, input("List of numbers: ").split(",")))
List of numbers: 3,4,52,3,2,1
>>> arr
[3, 4, 52, 3, 2, 1]