When would we use Dictionaries in Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 78

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! 🎉👏😊👍😀🎉

7th Aug 2018, 10:22 PM
Christine
Christine - avatar
56 Answers
+ 68
Christine you could use it like this my_best_friend = {'name': 'Christine', 'age': 18, 'nationality': 'USA', 'gender': 'F', 'hubby': 'programming', 'language': 'python' }
8th Aug 2018, 6:09 AM
E_E Mopho
E_E Mopho - avatar
+ 61
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'))()
20th Aug 2018, 3:44 PM
Anna
Anna - avatar
+ 46
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'
8th Aug 2018, 1:24 PM
Ahmad Muhammad
Ahmad Muhammad - avatar
+ 20
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! 😅
7th Aug 2018, 11:14 PM
KhadNeat
KhadNeat - avatar
+ 20
When you have a set of unique keys that map to particular values.
8th Aug 2018, 3:18 AM
Corey
Corey - avatar
+ 13
To store data. Or return multiple values.
8th Aug 2018, 7:45 AM
Toni Isotalo
Toni Isotalo - avatar
+ 9
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
20th Aug 2018, 3:36 PM
RZK 022
RZK 022 - avatar
+ 6
Python Dictionary reminds me Javascript object... what do you all think about it?
8th Aug 2018, 12:53 PM
Daniel Fernandes
Daniel Fernandes - avatar
+ 6
when you need to access any item in O(1) time or when your list has non scalar keys (ex c["green"]="red")
8th Aug 2018, 2:10 PM
VcC
VcC - avatar
+ 6
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.
10th Aug 2018, 7:00 PM
Apple Blossom
Apple Blossom - avatar
+ 6
dictionaries make it easier to associate (and later access) data with different keys.
20th Aug 2018, 3:11 PM
Ish Rogers
Ish Rogers - avatar
+ 5
https://code.sololearn.com/cVvH1pXc2nAw/?ref=app They make code shorter but make the program run slower. This code is an example.
20th Aug 2018, 3:52 PM
Poet🎭
Poet🎭 - avatar
+ 5
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...
20th Aug 2018, 7:55 PM
Mad Max
Mad Max - avatar
+ 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)
21st Aug 2018, 3:38 AM
Najum
Najum - avatar
+ 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.
21st Aug 2018, 4:13 AM
Sonic
Sonic - avatar
+ 4
salam
8th Aug 2018, 5:34 AM
Wepa
+ 4
Dictionary is simplier: person = {'name': 'Rocky', 'age': '21'} than list: person = ['name', 'age'] person_info = ['Rocky', '21']
20th Aug 2018, 3:59 PM
Email Not Activated
+ 4
Python dictionaries remind me of associative arrays in PHP.
21st Aug 2018, 3:13 AM
Sonic
Sonic - avatar
+ 3
var names=new Array('Ahmed','Abshir','dhore') alert(names)
20th Aug 2018, 3:46 PM
Ahmed Abshir Dhore
Ahmed Abshir Dhore - avatar
+ 3
I was going to say, "when you don't know the meaning of a word" 😂 but it's not that funny. I find that being able to refer to values at if I were dealing with a record is helpful. Python recognizes dicts naturally. Many libraries will output dicts, which makes passing information easy. Dicts are very similar to JSON, and can be used interchangably, which makes it very convenient. An example: I needed something to list files from AWS S3 buckets, that I would have to read and manipulate. I found someone's script that reads and delivers JSON, which I pick up as a dict, with multiple entries. I can access any element by name. it's all taken care of for me. I can add to the elements without having to change the structure, I can read or change a dict, and pass the whole thing without having to manipulate the data structure other than the standard commands. It's very convenient. Dicts can contain other data structures as their data, and you can refer to that element by name.
20th Aug 2018, 5:59 PM
JoeSponge
JoeSponge - avatar