Python Dictionaries | ||||||||||||||||||||||||
| ||||||||||||||||||||||||
Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered*, changeable and do not allow duplicates. Dictionaries are written with curly brackets, and have keys and values:
Dictionary Items Dictionary items are ordered, changeable, and does not allow duplicates. Dictionary items are presented in key:value pairs, and can be referred to by using the key name.
Ordered or Unordered? When we say that dictionaries are ordered, it means that the items have a defined order, and that order will not change. Unordered means that the items does not have a defined order, you cannot refer to an item by using an index. Changeable Dictionaries are changeable, meaning that we can change, add or remove items after the dictionary has been created. Duplicates Not Allowed Dictionaries cannot have two items with the same key:
Dictionary Length To determine how many items a dictionary has, use the
Dictionary Items - Data Types The values in dictionary items can be of any data type:
type() From Python's perspective, dictionaries are defined as objects with the data type 'dict':
The dict() Constructor It is also possible to use the dict() constructor to make a dictionary.
Accessing Items You can access the items of a dictionary by referring to its key name, inside square brackets:
There is also a method called
Get Keys The
The list of the keys is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the keys list.
Get Values The
The list of the values is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the values list.
Add a new item to the original dictionary, and see that the values list gets updated as well:
Get Items The
The returned list is a view of the items of the dictionary, meaning that any changes done to the dictionary will be reflected in the items list.
Add a new item to the original dictionary, and see that the items list gets updated as well:
Check if Key Exists To determine if a specified key is present in a dictionary use the
Change Values You can change the value of a specific item by referring to its key name:
Update Dictionary The The argument must be a dictionary, or an iterable object with key:value pairs.
Adding Items Adding an item to the dictionary is done by using a new index key and assigning a value to it:
Update Dictionary The The argument must be a dictionary, or an iterable object with key:value pairs.
Removing Items There are several methods to remove items from a dictionary: The pop() method removes the item with the specified key name:
The popitem() method removes the last inserted item (in versions before 3.7, a random item is removed instead):
The del keyword removes the item with the specified key name:
The
Loop Through a Dictionary You can loop through a dictionary by using a When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.
Copy a Dictionary You cannot copy a dictionary simply by typing There are ways to make a copy, one way is to use the built-in Dictionary method
Nested Dictionaries A dictionary can contain dictionaries, this is called nested dictionaries.
Or, if you want to add three dictionaries into a new dictionary:
Access Items in Nested Dictionaries To access items from a nested dictionary, you use the name of the dictionaries, starting with the outer dictionary:
Dictionary Methods Python has a set of built-in methods that you can use on dictionaries.
Below YouTube content is helpful for better understanding:
Tags: Python Python Dictionaries | ||||||||||||||||||||||||
| ||||||||||||||||||||||||
| ||||||||||||||||||||||||
|
OTHER POSTS IN THE SAME CATEGORYRegExp example in Python to exclude javascript from HTML codePython code to convert from Lunar to SolarPython example to download webpagePython Tutorials for AP Computer Science Principles, Data Projects and High School InternshipPython ModulesPython ScopePython PolymorphismPython IteratorsPython InheritancePython Classes/ObjectsPython ArraysPython LambdaPython FunctionsPython While Loops/For LoopsPython Conditions and If statementsPython SetsPython TuplesPython Comparison OperatorsPython Arithmetic OperatorsPrinting string n timesString concatenation by join()Python ListsPython String OperationsPython Data TypesPython VariablesPython CommentsPython SyntaxPython Getting StartedPython IntroductionWhat is Python? |