Ask about an error that occurred while printing the python list as *

Asked 2 years ago, Updated 2 years ago, 334 views

Hello. I have a question because the output of the Python list is confusing in many ways. If the elements in the list are integers, * is often used to print out the brackets, but this time I tried to apply the same method to the list sorted by the function, but I got an error like ValueError: invalid literal for int() with base 10: '&' . I printed it out because I thought the element type in the list might have changed, but I was more confused because I thought the integer type was right. I would appreciate it if you could tell me where the problem occurred.

def sel_sort(a):
    for i in range(len(a)-1):
        min_idx = i
        for j in range(i, len(a)):
            if a[j] < a[min_idx]:
                min_idx = j
        a[i], a[min_idx] = a[min_idx], a[i]
    return

lst = [3, 2, 1, 4, 5, 9, 8, 10, 6, 7]
sel_sort(lst)
print(type(lst[0]))
print(*lst)

python list output

2022-10-10 01:00

1 Answers

The error ValueError: invalid literal for int() with base 10: '&' is an error that occurs when trying to type the string "&" into int.

int("&")

If you run this code, you will be able to see the same error.

Also, unpacking can be used regardless of the data type inside the list.

lst = ["1", 2, 3.4, None]
print(*lst)


2022-10-10 01:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.