+ 1

Length function in python

a = ([{}]) print (len(a)) #outputs 1. Can somebody explain why it is so???

20th Jan 2021, 10:14 AM
CHANDAN ROY
CHANDAN ROY - avatar
1 Réponse
+ 15
{} is an element of the list [ { } ] ---> a list with an empty dictionary as its element. So even if the dictionary is empty, we are counting the number of elements of the list and not the dictionary inside it. Another Example: a = [ { }, { } ] print(len(a)) >> 2
20th Jan 2021, 10:15 AM
noteve
noteve - avatar