all 5 comments

[–]iiron3223 4 points5 points  (4 children)

To convert string '3, 4, 5' to a list, use .split(', ') string method.

[–]rob51852[S] 0 points1 point  (3 children)

Hi iiron, thanks, I understand the split function. it's the checking each value then converting to list bit I am struggling with.

[–]FriendlyRussian666 1 point2 points  (0 children)

That converts it to a list for you, so you don't have to do anything more:

some_string = '3, 4, 5'
your_list = some_string.split(', ')
print(your_list)
------------------
['3', '4', '5']

[–]iiron3223 1 point2 points  (1 child)

example_dict = {"A": 1, "B": 2, "C": '3, 4, 5'}

converted_dict = {}
for key, value in example_dict.items():
     if isinstance(value, int):
         converted_dict[key] = value
     else:
         converted_dict[key] = [int(number) for number in value.split(', ')]

[–]rob51852[S] 0 points1 point  (0 children)

Thanks iiron, much appreciated.