Q. Predict the output of following code fragment:
fruit = {}
fl = ['Apple', 'Banana', 'apple', 'Banana']
for index in fl:
if index in fruit:
fruit[index] += 1
else:
fruit[index] = 1
print (fruit)
print (len(fruit))
Answer:-
{'Apple': 1}
{'Apple': 1, 'Banana': 1}
{'Apple': 1, 'Banana': 1, 'apple': 1}
{'Apple': 1, 'Banana': 2, 'apple': 1}
3
>>>
{'Apple': 1, 'Banana': 1}
{'Apple': 1, 'Banana': 1, 'apple': 1}
{'Apple': 1, 'Banana': 2, 'apple': 1}
3
>>>
This program is counting the number of elements in the list and stores it in a dictionary.
How???
ReplyDeletePlease run this code in your IDLE Python.
DeleteCan u explain
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )