I read that my_dict.keys() returns the dynamic view to iterate over the dictionary. I usually iterate the dictionary without the keys() function.
So my question is, are below two code blokes are same? if not, what performance differences do these two have (which one is more optimized)
# without keys() function
my_dict = {'key1' : 'value1',"key2" : "value2"}
for key in my_dict:
print("current key is",key)
# withkeys() function
my_dict = {'key1' : 'value1',"key2" : "value2"}
for key in my_dict.keys():
print("current key is",key)
Note: I usually use python version 3.7+ so if there's any version-wise implementation difference, kindly attach resources that I can refer to.
source https://stackoverflow.com/questions/74803285/is-these-two-dictonary-statments-are-same-while-looping-it-in-for-loop
Comments
Post a Comment