0
ct_dict = {
    'fr': ['55410', '94560', '52533', '63040', '52533'],
    'de': ['73546', '64090', '60938', '75648', '60938']}

How can i change the values inside the dictionary from string to integer? For example: ['55410', '94560', '52533', '63040', '52533'] to [55410, 94560, 52533, 63040, 52533]

I tried using 2 for loops with update method but got this error:

RuntimeError: dictionary changed size during iteration

Code that got the error:

for i in ct_dict:
  for j in ct_dict[i]:
    ct_dict.update({j:int(j)})
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Yuvi
  • 19
  • 2
    Use a dict comprehension instead. Or put the result into a new dict instead of `ct_dict`. – luk2302 May 31 '23 at 20:58
  • The values are **lists of strings**, not strings themselves, so you'll need to rethink your approach. (Although, `ct_dict.update({j: ...})` is wrong anyway.) Think about how to [convert a list of strings to int](/q/7368789/4518341). – wjandrea May 31 '23 at 21:04
  • @luk2302 That's not necessary at all. OP's just going about this wrong. – wjandrea May 31 '23 at 21:05

4 Answers4

1

The keys of ct_dict are i, not j, so you shouldn't use j in the .update() method.

But if you're just updating one key, just assign to that key rather than using .update().

You need to map int() over all the elements of the list.

for i, vals in ct_dict.items():
    ct_dict[i] = list(map(int, vals))
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

You don't actually need to update the dictionary, just change the values in the lists directly:

for nums in ct_dict.values(): 
    nums[:] = map(int,nums)
Alain T.
  • 40,517
  • 4
  • 31
  • 51
0

You're getting an error because you're trying to modify the dict you're using as a reference in the for loop, while the loop is executing, and you can't do that.

Solution

Assuming:

ct_dict = {
        "fr": ["55410", "94560", "52533", "63040", "52533"],
        "de": ["73546", "64090", "60938", "75648", "60938"],
    }

Best option is use to dict comprehension:

new_ct_dict = {key: [int(i) for i in values] for key, values in ct_dict.items()}

But you can also do:

new_ct_dict = {}
for key, values in ct_dict.items():
    new_ct_dict.update({key: []})
    for i in values:
        new_ct_dict[key].append(int(i))

Result:

{'fr': [55410, 94560, 52533, 63040, 52533], 'de': [73546, 64090, 60938, 75648, 60938]}
Riqq
  • 137
  • 1
  • 4
  • 1
    Although this works, this is not a good answer - it doesn't really address OP's problem other than avoiding it with alternative code and provide no explanation of why this works and why what OP did wsould not. Have a look at [How to answer](https://stackoverflow.com/help/how-to-answer) – Grismar May 31 '23 at 21:06
  • 1
    `new_ct_dict.update({key: []})` should be `new_ct_dict[key] = []` – wjandrea May 31 '23 at 21:09
  • 1
    You don't need to create a new dict when you can update the existing one, like in [Barmar's answer](/a/76377300/4518341). – wjandrea May 31 '23 at 21:12
0

Use a for loop and a list comprehension:

my_dict = {'fr': ['55410', '94560', '52533', '63040', '52533'], 'de': ['73546', '64090', '60938', '75648', '60938']}
new_dict = {}

for key, val in my_dict.items():
    new_dict[key] = [int(item) for item in val]

print(new_dict)  # {'fr': [55410, 94560, 52533, 63040, 52533], 'de': [73546, 64090, 60938, 75648, 60938]}

Or, if you'd rather not create another dict:

my_dict = {'fr': ['55410', '94560', '52533', '63040', '52533'], 'de': ['73546', '64090', '60938', '75648', '60938']}

for key, val in my_dict.items():
    my_dict[key] = [int(item) for item in val]

print(my_dict)  # {'fr': [55410, 94560, 52533, 63040, 52533], 'de': [73546, 64090, 60938, 75648, 60938]}

For more on list comprehensions see: https://www.w3schools.com/python/python_lists_comprehension.asp

For more on the dict.items() method see: https://www.w3schools.com/python/ref_dictionary_items.asp