0

When would I need to use a dictionary in a "real-world" program?(python)

Just started learning about them, but I don't really see a situation in which using them is necessary.

17th Jan 2018, 6:11 AM
BathrobeOverlord69
BathrobeOverlord69 - avatar
3 Respostas
+ 3
If you want to make a list of friends. And you want to store information of them. You could have a list of dictionaries with your friends. Every dictionary is a person and it has age, gender, hairColor, eyesColor, etc... friend1 = {"name": "John", "age":18.....} This way you can access to his attributes fast. friend1["age"] returns 18.
17th Jan 2018, 6:51 AM
Sebastián Zapata
Sebastián Zapata - avatar
+ 2
I should say no..., but can be thinked something like that :). They're just a "list" with values that you access passing a key and its elements has no order (they're not sortable in order to speed up their search). In lists (or tuples or sets) you pass the index to get an element but you "don't know" what value can be returned. In dictionaries you "know" what you are searching for, and you pass just the key and you get the value associated with that key. Python has classes and you can set attributes and define methods but dictionaries are just that above and can't do more.
17th Jan 2018, 11:29 AM
Sebastián Zapata
Sebastián Zapata - avatar
0
Got it! so basically Dictionaries in python work simillarly to methods in other programming languages?
17th Jan 2018, 7:50 AM
BathrobeOverlord69
BathrobeOverlord69 - avatar