Top 10 Programming Languages to Learn in 2019

ProgrammingPythonPython Basic Tutorial

Python Dictionary


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.

What is Python Dictionary?

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.

Python Dictionary Creation

>>> # Creating a dictionary with {} and two key:value pairs
>>> phone = {'aditya': 1234567890, 'python':1000365289}
>>> phone
{'aditya': 1234567890, 'python': 1000365289}
>>> phone['python']
>>> # 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'}

Memory Representation of Python Dictionary

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

Python Dictionary Methods

Python dictionary comes with a huge list of inbuilt methods. Here are some most commonly used methods.

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 ūüėĀ.

Related posts
ProgrammingPythonPython Basic Tutorial

Mastering Print Formatting in Python: A Comprehensive Guide


Global Variables in Python: Understanding Usage and Best Practices

ProgrammingPythonPython Basic Tutorial

Secure Your Documents: Encrypting PDF Files Using Python


Creating and Modifying PDF Files in Python: A Comprehensive Guide with Code Examples

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.