Getting error while popping in dictionary python
Question:
I have a dictionary
price={'10MAR23': 113.49999999999999,
'3MAR23': 19.10000000000001,
'25FEB23': 19.100000000000012,
'28APR23': 3.5,
'26FEB23': 0.4,
'31MAR23': 27.5,
'17MAR23': 3.1}
I want to replace the keys with respective dates with format as %d%m%y like
100323
030323
250223
280423
260223
310323
170323
The following code does that,
for i in price:
new_key=datetime.datetime.strptime(i,'%d%b%y').strftime('%d%m%y')
price[new_key]=price.pop(i)
but getting error like
ValueError: time data '030323' does not match format '%d%b%y'
Answers:
This happens because you are looping over price
while modifying its keys at the same time. You can avoid that by copying the dictionary before performing the loop, with for i in dict(price):
, as so:
price = {'10MAR23': 113.49999999999999,
'3MAR23': 19.10000000000001,
'25FEB23': 19.100000000000012,
'28APR23': 3.5,
'26FEB23': 0.4,
'31MAR23': 27.5,
'17MAR23': 3.1}
for i in dict(price):
new_key = datetime.datetime.strptime(i,'%d%b%y').strftime('%d%m%y')
price[new_key] = price.pop(i)
Alternatively, you can create a new dictionary from the old one with a dictionary comprehension
price = {datetime.datetime.strptime(k, '%d%b%y').strftime('%d%m%y'): v
for k, v in price.items()}
The error you are getting is because you are trying to change the keys of a dictionary while iterating over it. This modifies the size of the dictionary and causes an inconsistency in the loop.
One way to avoid this error is to create a copy of the original dictionary and iterate over that instead. Then you can use the pop() function to remove the old key and add the new key with the same value. For example:
price_copy = price.copy() for i in price_copy: new_key=datetime.datetime.strptime(i,‘%d%b%y’).strftime(‘%d%m%y’) price[new_key]=price.pop(i)
Another way is to use a dictionary comprehension to create a new dictionary with the updated keys. For example:
price = {datetime.datetime.strptime(i,‘%d%b%y’).strftime(‘%d%m%y’): v for i, v in price.items()}
I have a dictionary
price={'10MAR23': 113.49999999999999,
'3MAR23': 19.10000000000001,
'25FEB23': 19.100000000000012,
'28APR23': 3.5,
'26FEB23': 0.4,
'31MAR23': 27.5,
'17MAR23': 3.1}
I want to replace the keys with respective dates with format as %d%m%y like
100323
030323
250223
280423
260223
310323
170323
The following code does that,
for i in price:
new_key=datetime.datetime.strptime(i,'%d%b%y').strftime('%d%m%y')
price[new_key]=price.pop(i)
but getting error like
ValueError: time data '030323' does not match format '%d%b%y'
This happens because you are looping over price
while modifying its keys at the same time. You can avoid that by copying the dictionary before performing the loop, with for i in dict(price):
, as so:
price = {'10MAR23': 113.49999999999999,
'3MAR23': 19.10000000000001,
'25FEB23': 19.100000000000012,
'28APR23': 3.5,
'26FEB23': 0.4,
'31MAR23': 27.5,
'17MAR23': 3.1}
for i in dict(price):
new_key = datetime.datetime.strptime(i,'%d%b%y').strftime('%d%m%y')
price[new_key] = price.pop(i)
Alternatively, you can create a new dictionary from the old one with a dictionary comprehension
price = {datetime.datetime.strptime(k, '%d%b%y').strftime('%d%m%y'): v
for k, v in price.items()}
The error you are getting is because you are trying to change the keys of a dictionary while iterating over it. This modifies the size of the dictionary and causes an inconsistency in the loop.
One way to avoid this error is to create a copy of the original dictionary and iterate over that instead. Then you can use the pop() function to remove the old key and add the new key with the same value. For example:
price_copy = price.copy() for i in price_copy: new_key=datetime.datetime.strptime(i,‘%d%b%y’).strftime(‘%d%m%y’) price[new_key]=price.pop(i)
Another way is to use a dictionary comprehension to create a new dictionary with the updated keys. For example:
price = {datetime.datetime.strptime(i,‘%d%b%y’).strftime(‘%d%m%y’): v for i, v in price.items()}