🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Create a Dictionary in Python!
🖥️ How to Create a Dictionary in Python?
Creating a dictionary in Python is as simple as placing “key: value” items inside curly braces {}, and separated by comma. Each key is separated from its associated value by a colon “:”.
Let’s have a closer look at the syntax of a Dictionary in Python
Here is an example, in which you want to store an employee record. First, Let’s declare an empty dictionary – using the curly braces {}. Eventually, you will then add all of the personal information about the employee, so you can store its information and then access it later.
employee = {
'age': 27,
'city': 'Paris',
'job': 'Youtuber',
'name': 'Digital Academy',
'email': 'digital.academy@free.fr'
}
You can also convert two-value sequences into a dictionary, using the built-in function dict() in Python. The first item inside each sequence is then used as the key, and the second as the value.
# Using dict() constructor
my_dict = dict({'name':'apple', 2:'strawberry'})
# From sequence, having each item as a pair
my_dict = dict([('name','Digital Academy'), ('age',27)]) # List of Tuples
my_dict = dict((['name','Digital Academy'], ['age',27])) # Tuple of Lists
# my_dict = {'name': 'Digital Academy', 'age': 27}
When the keys are simple strings, it is sometimes easier to specify key:value pairs, using keyword arguments.
my_dict = dict(
age = 27,
job = 'Youtuber',
name = 'Digital Academy'
)
# my_dict = {'name': 'Digital Academy', 'age': 27, 'job': 'Youtuber'}
—
Dictionaries are pretty straightforward, but here are a few points you should be aware of:
○ Keys must be unique
A key can appear in a dictionary only once. Even if you specify a key more than once during the creation of a dictionary, the last value for that key becomes the associated value.
my_dict = {
'age': 27,
'name': 'Jérémy',
'name': 'Digital Academy'
}
# my_dict = {'name': 'Digital Academy', 'age': 27}
○ Key must be Immutable type
You can use any object of immutable type as dictionary keys – such as numbers, strings, booleans, tuples. Otherwise an exception is raised, when mutable object is used as a key.
my_dict = {
0: 1, # Number
True: 'a', # Boolean
(2,2): 25, # Tuple
[2,2]: 25, # TypeError: unhashable type: 'list'
'name': 'Digital Academy' # String
}
○ Value can be of any type
There are no restrictions on dictionary values. A dictionary value can be any type of object, and can even appear in a dictionary, multiple times.
# Values of different datatypes
my_dict = {
'a': [1,2,3], # List
'b': {1,2,3} # Set
}
# Duplicate values
my_dict = {
'a': [1,2], # List a
'b': [1,2], # List b
'c': [1,2] # List c
}
Let’s play this video, stick around and watch until the end of this video! 👍🏻
– Digital Academy™ 🎓
***
☞ WATCH NEXT:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho
☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw
📖 Blog: http://digital.academy.free.fr/blog/
📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners
🛒Shopping and Discounts: http://digital.academy.free.fr/store/
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
✧ http://digital.academy.free.fr/
✧ https://twitter.com/DigitalAcademyy
✧ https://www.facebook.com/digitalacademyfr
✧ https://www.instagram.com/digital_academy_fr/
✧ https://www.youtube.com/c/DigitalAcademyOnline
♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧ https://www.patreon.com/digital_academy
✧ https://www.buymeacoffee.com/digital_academy
***