When would we use Dictionaries in Python? | Sololearn: Learn to code for FREE!


When would we use Dictionaries in Python?

I just started learning about dictionaries in Python and I get how it works. But why would we use these and in what circumstances would we use dictionaries? What kinds of cool codes does dictionaries help make? Thanks! 😉😀👍😊👏 EDIT: Thank you everyone for your help and information on this! I now understand it more. Case SOLVED! 🎉👏😊👍😀🎉

8/7/2018 10:22:19 PM


54 Answers

New Answer


Christine you could use it like this my_best_friend = {'name': 'Christine', 'age': 18, 'nationality': 'USA', 'gender': 'F', 'hubby': 'programming', 'language': 'python' }


Most programming languages have a switch statement: switch (variable) { case 0: dothis(); break; case 1: dothat(); break; default: donothing() } Python doesn't have that, but you can use a dict instead: what_to_do = { 0: 'dothis', 1: 'dothat', } eval(what_to_do.get(variable, 'donothing'))()


very similar to lists : my_fav_apps = ['sololearn', 'chrome'] when we want to access one of it's items we use number (indexs) : my_fav_apps[0] = 'sololearn' my_fav_apps[1] = 'chrome' but in DICTIONARIES we use keys instead to make it more easier and readable .. my_fav_apps = {'edu': 'sololearn', 'browser': 'chrome', } ... to access it's data : my_fav_apps ['edu'] == 'sololearn' my_fav_apps ['browser'] == 'chrome' you can do a trick to access list items using keys like dictionaries Note : in this way keys are VARIABLES not a STRING . #this is a python code ;) app = ['sololearn', 'chrome'] edu = 0 browser = 1 #output print( app[ edu ] ) #prints 'sololearn' print( app[ browser ] ) #prints 'chrome'


When you have a set of unique keys that map to particular values.


I'm also learning Python 🐍! Congratulations, colleague! 🙏 Dictionaries can use them when you need to use elements, which have a definition. For example: To define capitals of the world, data of a person, animal, etc. Hope this information is helpful! Thank you very much! 😅


To store data. Or return multiple values.


use dictionaries when you want a list to not be defined in order by integer index but instead want to use some different value , string integer or float. like in list it's like ar[index] = value. in dictionary it's : dct[key] = value. after you know how to use dictionaries you'll automatically get to know when and where to use it, just keep challenging yourself with various programming problems. After I learned dictionary in python I had created this code a week later, so it was easy to figure it out : https://code.sololearn.com/c2lLa1kUrn5Z/?ref=app it could've been much difficult without dictionaries, I mean in that I had to use switch or if statements, and I'm darn lazy to write that all


Python Dictionary reminds me Javascript object... what do you all think about it?


when you need to access any item in O(1) time or when your list has non scalar keys (ex c["green"]="red")


dictionaries make it easier to associate (and later access) data with different keys.


Dictionaries are basically a version of an Array. They can be used to store information. Try combining a Dictionary using Index-Sorting or Bubble-Sorting for different results.


https://code.sololearn.com/cVvH1pXc2nAw/?ref=app They make code shorter but make the program run slower. This code is an example.


Dictionaries have many advantages: 1. They act as hash tables, allowing access to a member in O(1) 2. They are useful for categorization and classification 3. Since each key is unique, they can be used to sort and count instances. However there are disadvantages too: 1. They are not sorted, they preserve insertion order 2. They are not efficient for mathematical operations 3. They don't support slicing 4. While dicts are mutable, their key:value pairs are not (they are tuples)


Yes O(1) access time is a huge performance advantage for dictionaries and hash maps which means that regardless of the number of entries in the dictionary it will take roughly a constant amount of time to access the value related to any key.




var names=new Array('Ahmed','Abshir','dhore') alert(names)


Dictionary is simplier: person = {'name': 'Rocky', 'age': '21'} than list: person = ['name', 'age'] person_info = ['Rocky', '21']


Dictionaries are based on the concept of key:value pairs... If you have used json or another data transfer modes such as xml... you would know the importance of it... areas of application include. 1. Web service calls and data transfer 2. Data warehouse and mining. 3. Analytics(<10mb) 4. Webapps etc...


Python dictionaries remind me of associative arrays in PHP.


what is you name