Python Dictionary is one of the most used data structure in Python. It is similar to hash tables, associative memories or associative arrays in other programming languages. Python Dict is an unordered collection of zero or more key-value pairs whose keys are object references that refer to hashable objects.
Dictionaries by default are mutable and can be easily edited, but since they are unordered they have no notion of index position and therefore they cannot be sliced or stridden. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type. Strings and numbers can always be keys.
Tuples can also be used as keys for dictionaries if they contain numbers, strings or tuples. Just make sure that the datatype you use for the key must not be directly mutable.
Learn more about Python Strings. New to Python? Go through our brief introduction to Python to get you started.
It is best to think of a python dictionary as a set of key: value pairs, with the requirement that the keys are unique (within one dictionary). The dict datatype can be called as a function, dict(). With no argument, it returns an empty dictionary.
The main operation of Python Dictionaries is to store a value with a given key are extracting the value with the help of the key. You can use del keyword to delete a key-value pair.
Now let’s go through few examples to make you understand better.
>>> # Creating a dictionary with {} and two key:value pairs >>> phone = {'aditya': 1234567890, 'python':1000365289} >>> phone {'aditya': 1234567890, 'python': 1000365289} >>> phone['python'] 1000365289 >>> # Adding New objects in Dictionary >>> phone['new'] = 5555555555 >>> phone {'aditya': 1234567890, 'python': 1000365289, 'new': 5555555555}
Python Dictionaries are very flexible in the data types that they can hold. We can use list(d)
on a dictionary to return a list of all the keys used in the dictionary, in insertion order.
Few other ways to create Dictionaries, We will be creating same dictionary but using different ways:
>>> d1 = dict({'name': 'python', 'size': 'five'}) >>> d2 = dict(name= 'python', size= 'five') >>> d3 = dict([("name", "python"), ("size", "five")]) >>> d4 = dict(zip(("name", "size"), ("python", "five"))) >>> d5 = {'name': 'python', 'size': 'five'} >>> d1 {'name': 'python', 'size': 'five'} >>> d2 {'name': 'python', 'size': 'five'} >>> d3 {'name': 'python', 'size': 'five'} >>> d4 {'name': 'python', 'size': 'five'} >>> d5 {'name': 'python', 'size': 'five'}
It is important to note that Python Dictionaries are very flexible with data types they can hold. Most of the time you will be using Dictionaries with the conjunction of other data types such as lists, dictionaries, tuples.
>>> programming_lang = {'name':"Python", "id":1, "version":3.6, "frameworks":{"web frameworks":"django", "ML":"sklearn"}} >>> programming_lang {'name': 'Python', 'id': 1, 'version': 3.6, 'frameworks': {'web frameworks': 'django', 'ML': 'sklearn'}} >>> programming_lang['frameworks'] {'web frameworks': 'django', 'ML': 'sklearn'}
Python dictionary comes with a huge list of inbuilt methods. Here are some most commonly used methods.
Syntax | Description |
d.clear() | Removes all the items from dict d. |
d.copy() | Returns a shallow copy of dict d. |
d.fromkeys(s, v) | Returns a dict whose keys are the items in the items in sequence s and whose values are None or v if v is given. |
d.get(k) | Returns key’s k associated value or returns None if k isn’t in dict d. |
d.get(k, v) | Returns key k‘s associated value, or v if k isn’t in dict d. |
d.items() | Returns a view of all the (key, value) pairs in dict d. |
d.keys() | Returns a view of all the keys in dict d. |
d.pop() | Returns key’s k associated value and deletes the item whose key is k. |
d.pop() | Returns key’s k associated value and deletes the item whose key is k or v if k isn’t in keys. |
d.update(a) | Adds every (key, value) pair form a that isn’t in dict d. |
d.values() | Returns a view of all the values in dict d. |
That’s it, folks! If you face any issue or need any help drop down a comment. I will revert back to you 😁.
In Python, the print() function is a fundamental tool for displaying output. While printing simple…
Python is a versatile programming language known for its simplicity and flexibility. When working on…
PDF (Portable Document Format) files are commonly used for sharing documents due to their consistent…
PDF (Portable Document Format) files are widely used for document exchange due to their consistent…
Python is a high-level programming language known for its simplicity and ease of use. However,…
Object-Oriented Programming (OOP), iterators, generators, and closures are powerful concepts in Python that can be…
This website uses cookies.