Categories: Programming Python Python 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.

Related Post

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 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 😁.

Aditya Kumar

Share
Tags: Python Python Basic Tutorial

Recent Posts

  • Programming

Mastering Print Formatting in Python: A Comprehensive Guide

In Python, the print() function is a fundamental tool for displaying output. While printing simple…

8 months ago
  • Programming

Global Variables in Python: Understanding Usage and Best Practices

Python is a versatile programming language known for its simplicity and flexibility. When working on…

8 months ago
  • Programming

Secure Your Documents: Encrypting PDF Files Using Python

PDF (Portable Document Format) files are commonly used for sharing documents due to their consistent…

8 months ago
  • Programming

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

PDF (Portable Document Format) files are widely used for document exchange due to their consistent…

8 months ago
  • Programming

Boosting Python Performance with Cython: Optimizing Prime Number Detection

Python is a high-level programming language known for its simplicity and ease of use. However,…

8 months ago
  • Programming

Using OOP, Iterator, Generator, and Closure in Python to implement common design patterns

Object-Oriented Programming (OOP), iterators, generators, and closures are powerful concepts in Python that can be…

8 months ago

This website uses cookies.