Top 10 Programming Languages to Learn in 2019

ProgrammingPythonPython Basic Tutorial

Python Dictionary

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']
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'}

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.

SyntaxDescription
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
Programming

Amazon Price Scraper and Auto Mailer Python App

ProgrammingPython

Top 10 Deep Learning frameworks in 2019 (with comparison)

ProgrammingPython

Python vs R - Data Visualization

Programming

Cython vs Python - Speed up your Python

Leave a Reply

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

Improve Your Python

...with a fresh 🐍 Python Trick 💌
code snippet every couple of days:

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

PyBlog will use the information you provide on this form to be in touch with you and to provide updates and marketing.