filmov
tv
How to Create a Dictionary in Python? - Python Tutorial for Beginners

Показать описание
🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!
🖥️ 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',
}
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:
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
♡ SUPPORT US ♡
***
🖥️ 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',
}
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:
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
♡ SUPPORT US ♡
***
Комментарии