PYTHON DICTIONARY:
Dictionaries are commonly used for representing structured data, such as:
- Storing configuration settings
- Managing user data in applications
- Representing JSON (JavaScript Object Notation) data
- Counting occurrences of items
EXAMPLE:
CREATING DICTIONARY:
You can create a dictionary in Python using curly braces '{}' and specifying key-value pairs separated by colons.
Here's a step-by-step explanation of creating a dictionary:
- Open a curly brace '{' to start the dictionary.
- Specify key-value pairs inside the dictionary. Each pair consists of a key followed by a colon: and then the associated value.
- Separate multiple key-value pairs with commas.
- Close the curly brace '}' to finish defining the dictionary.
EXAMPLE:
ACCESSING VALUE FROM DICTIONARY:
EXAMPLE:
EXAMPLE:
BUILT-IN DICTIONARY FUNCTION:
Sr.No. |
Function with Description |
1 |
Compares elements of both
dicts. |
2 |
Gives the total length of the
dictionary. This would be equal to the number of items in the dictionary. |
3 |
Produces a printable string
representation of a dictionary |
4 |
Returns the type of the passed
variable. If a passed variable is a dictionary, then it would return a dictionary
type. |
BUILT-IN METHOD IN DICTIONARY:
Sr.No. |
Methods with Description |
1 |
Removes all elements of the dictionary dict |
2 |
Returns a shallow copy of the dictionary dict |
3 |
Create a new dictionary with keys from seq and values set to value. |
4 |
For key, returns value or default if key not in the dictionary |
5 |
Returns true if key in dictionary dict, false otherwise |
6 |
Returns a list of dict's (key, value) tuple pairs |
7 |
Returns list of dictionary dict's keys |
8 |
dict.setdefault(key, default=None) Similar to get(), but will set dict[key]=default if the key is
not already in the dict |
9 |
Adds dictionary dict2's key-values pairs to dict |
10 |
Returns list of dictionary dict's values |
CREDITED BY @DiplomaWale
0 Comments