- We can us list, tuple and set to represent a group of individual objects as a single entity.
- If we want to represent a group of objects as key-value pairs then we should go for dictionary.
Example
Python
student = {
'rollno': 'name'
}
client = {
'phone number': 'address'
}
site = {
'ipaddress': 'domain name'
}
Properties
- Duplicate keys are not allowed but values can be duplicated.
- Heterogeneous objects are allowed for both key and values.
- Insertion order is not preserved.
- Dictionaries are mutable.
- Dictionaries are dynamic.
- Indexing and slicing concepts are not applicable.
Creation of dictionary object
There are two methods to create dictionary
d={} or d=dict()
- We create emty dictionary using {} or dict().
Example
Python
d={}
d[1]="ajay"
d[2]="amit"
print(d)
d=dict()
d[1]="ajay"
d[2]="amit"
print(d)
Output
PowerShell
{1:'ajay', 2:'amit'}
{1:'ajay', 2:'amit'}
d={key:value}
- If we know data in advance then we can create dictionary using {key:value}.
Example
Python
d={1:'ajay', 2:'amit'}
print(d)
Output
PowerShell
{1:'ajay', 2:'amit'}
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!