0
stringIds = "948274432, 948364892, 943224012"

I have such a string. How can I transfer the ids here into a list by converting them to int?

intList = []
for x in stringIds:
    intList.append(int(x))

I tried a code like this but the error I got ValueError: invalid literal for int() with base 10: ','

Here is the sample list I want to get

intList = [948274432, 948364892, 943224012]
imuzz01
  • 27
  • 4
  • Does this answer your question? [How to split a string of space separated numbers into integers?](https://stackoverflow.com/questions/6429638/how-to-split-a-string-of-space-separated-numbers-into-integers) – sahasrara62 Jan 17 '23 at 23:46

1 Answers1

0

str.split can split the string at the commas, then you can apply int to each element to get them as numbers.

intList = [int(x) for x in stringIds.split(', ')]

or, written out as a for loop like in your example,

intList = []
for x in stringIds.split(', '):
    intList.append(int(x))
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116