Answers for "dictionary changed size during iteration after pop function"

0

dictionary changed size during iteration after pop function

cars = {
 "brand": "Tesla",
 "model": "Model S Plaid",
 "year":  2021
 }

for x in cars.keys():
  cars["color"] = "white"
print(x)
Posted by: Guest on January-23-2022
0

dictionary changed size during iteration after pop function

import copy
cars = {
 "brand": "Tesla",
 "model": "Model S Plaid",
 "year":  2021,
 }

#creating a shallow copy
cars_copy = copy.copy(cars)

for x in cars_copy.keys():
    cars["color"] = "black"
    
print(cars)
print(cars_copy)
Posted by: Guest on January-23-2022

Code answers related to "dictionary changed size during iteration after pop function"

Python Answers by Framework

Browse Popular Code Answers by Language