-1

How to return the length of a dictionary to a variable in python?

Drew
  • 13
  • 5
  • 2
    What did you try? The answer to this is rather straightforward... – Willem Van Onsem Mar 04 '18 at 21:53
  • 1
    Welcome to StackOverflow! Read about [how to ask a question](https://stackoverflow.com/help/how-to-ask) (particularly [how to create a good example](https://stackoverflow.com/help/mcve)) in order to get responses. You can also [take the tour](https://stackoverflow.com/tour). – Alex Mar 04 '18 at 21:54
  • ...oops [>> here it is <<](https://stackoverflow.com/a/2212442/8928024). – ZF007 Mar 04 '18 at 21:56

3 Answers3

1

Simply just use len():

dic = {'val1':0,'val2':0,'val3':0}
length = len(dic)
Zak Stucke
  • 432
  • 6
  • 18
1
my_dict = {'a':1,'b':2,'c':3}

var = len(dict)
0

my_var = len(my_dict)

Gets the number of keys in the dictionary. Equivalent to len(my_dict.keys()).

adapap
  • 665
  • 1
  • 9
  • 21