+ 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! 🎉👏😊👍😀🎉
57 odpowiedzi
+ 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'))()
+ 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'
+ 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! 😅
+ 20
When you have a set of unique keys that map to particular values.
+ 13
To store data. Or return multiple values.
+ 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
+ 6
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 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.
+ 6
dictionaries make it easier to associate (and later access) data with different keys.
+ 5
https://code.sololearn.com/cVvH1pXc2nAw/?ref=app
They make code shorter but make the program run slower. This code is an example.
+ 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...
+ 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
Dictionary is simplier:
person = {'name': 'Rocky', 'age': '21'}
than list:
person = ['name', 'age']
person_info = ['Rocky', '21']
+ 4
Python dictionaries remind me of associative arrays in PHP.
+ 3
var names=new Array('Ahmed','Abshir','dhore')
alert(names)
+ 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.