0

How i can change dictionary when it iterated in python?

7th Mar 2020, 7:57 PM
Vlad
Vlad - avatar
3 ответов
+ 3
What do you want to change in the dictionary? Key or value?
7th Mar 2020, 9:48 PM
Lothar
Lothar - avatar
+ 1
When looping over a dictionary directly, you'd go like this: for key in d: d[key]... and then whatever you want to do. You can't do anything that changes the size of your dict while you're looping over it. What exactly do you want to do?
7th Mar 2020, 8:10 PM
HonFu
HonFu - avatar
+ 1
For example (very simple code:)): a = {'a': 1, 'b': 2} for k, v in a.items(): del a[k]
7th Mar 2020, 8:16 PM
Vlad
Vlad - avatar