You update the dictionary while it is enumerated:
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
c = {'apples': 3, 'biscuits and tea': 3, 'oranges and onions': 4}
for k in c:
splits=k.split()
k_new= " ".join(lemmatizer.lemmatize(w.lower()) for w in splits)
c[k_new] = c.pop(k)
print(c)
updating a collection while you iterate over it is usually a very bad idea. Most data structures are not designed for this.
However, you can create a new dictionary:
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
c = {'apples': 3, 'biscuits and tea': 3, 'oranges and onions': 4}
c_new = {}
for k in c:
splits=k.split()
k_new= " ".join(lemmatizer.lemmatize(w.lower()) for w in splits)
c_new[k_new] = c[k]
print(c_new)
We can make it more elegant using a dictionary understanding:
{" ".join(lemmatizer.lemmatize(w.lower()) for w in k.split()): v
for k,v in c.items()}
, - k,v
c
" ".join(lemmatizer.lemmatize(w.lower()) for w in k.split())
, v
.