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

+72

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

Christine

57 Answers

New Answer

+51

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

+44

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'))()

+43

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'

+20

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

+15

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! 😅

+13

To store data. Or return multiple values.

+8

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

+7

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

+6

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

+6

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

+5

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.

+5

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

+5

#vowel check.. a="hello world!" vowels={'a ' : 0, 'e' : 0, 'i' : 0, 'o' : 0, 'u' : 0} for i in vowels: vowels[i] = a.count(i) print(vowels)

+5

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)

+5

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.

+4

salam

+4

what is you name

+4

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

+4

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...

+4

Python dictionaries remind me of associative arrays in PHP.