How to return the length of a dictionary to a variable in python?
Asked
Active
Viewed 458 times
-1
-
2What did you try? The answer to this is rather straightforward... – Willem Van Onsem Mar 04 '18 at 21:53
-
1Welcome 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 Answers
1
my_dict = {'a':1,'b':2,'c':3}
var = len(dict)

MrGenuis777
- 48
- 8
-
2never name your variables after built-ins or datatypes - like dict, list, set, tuple ... AND ... this ... is a set .... at least if you assing anything to the names `a,b,c` - else is is a `NameError`.. – Patrick Artner Mar 04 '18 at 21:58
-
-
1
-
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