1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How to delete items from a dictionary while iterating over it?

Discussão em 'Python' iniciado por Stack, Dezembro 1, 2025.

  1. Stack

    Stack Membro Participativo

    Can I delete items from a dictionary in Python while iterating over it?

    I want to remove elements that don't meet a certain condition from the dictionary, instead of creating an entirely new dictionary. Is the following a good solution, or are there better ways?

    for k, v in mydict.items():
    if k == val:
    del mydict[k]

    Continue reading...

Compartilhe esta Página