0

I'm having a problem here, its counting the empty lists which must not be counted

Is there a way to not count the empty lists?

dic ={None: [[], [], [(3, 2, 0)], [(3, 1, 0)], [], [], [(4, 3, 2), (4, 3, 0)], [(4, 2, 0)]]}
x = list(map(len, dic.values()))
print(x[0])

Output Required

5

My code Outpute

8
jack
  • 13
  • 1
  • 7

1 Answers1

3

dic.values() wraps all the values from different keys inside another list:

>>> dic.values()
[[[], [], [(3, 2, 0)], [(3, 1, 0)], [], [], [(4, 3, 2), (4, 3, 0)], [(4, 2, 0)]]]

Since, you have only one key there is only one element in dic.values() and when you do x = list(map(len, dic.values())) you get 8 because that's the length on the inner list.

You need to iterate over the inner list dic.values()[0] and get the length from there:

>>> sum(map(len, dic.values()[0]))
5

UPDATE: If you are using python 3, you can get the first value as list(dic.values())[0]:

>>> sum(map(len, list(dic.values())[0]))
5

This SO Post list many ways you can get the first value.

Community
  • 1
  • 1
AKS
  • 18,983
  • 3
  • 43
  • 54