Python Dictionary in detail

by | May 2, 2024 | Python | 0 comments

What is Dictionary in Python?

First of All it is not sequential like List. It is an non-sequential, unordered, redundant and mutable collection as key:value pairs. Keys are always unique but values need not be unique. You use the key to access the corresponding value. Where a list index is always a number, a dictionary key can be a different data type, like a string, integer, float, or even tuples.

Python dictionaries are unordered collections of key-value pairs. They are mutable, meaning you can add, remove, and modify elements after creation. Dictionary keys must be unique and immutable (e.g., strings, numbers, tuples), while values can be of any data type.

The contents of a dict can be written as a series of key:value pairs within braces { }, e.g.

dict = {key1:value1, key2:value2, … }.

The “empty dict” is just an empty pair of curly braces {}.

Differences between String and Dictionary

list = [1,’A’,2,3,2,8.7]

dict = {0:1,1:’A’,2:2,3:3,4:2,5:8.7}

List has numeric index only in sequential order starting from zero.

Dictionary is has keys instead of index which can be numeric, string or tuple. They are unordered but they are always unique.

0 1 2 3 4 # indexes are not there

Functions for Dictionaries

clear()dict.clear()Removes all items from the dictionary.my_dict = {‘a’: 1, ‘b’: 2}<br>my_dict.clear()
copy()dict.copy()Returns a shallow copy of the dictionary.my_dict = {‘a’: 1, ‘b’: 2}<br>new_dict = my_dict.copy()
get()dict.get(key[, default])Returns the value for the specified key. If the key is not found, returns the default value or None.my_dict = {‘a’: 1, ‘b’: 2}<br>value = my_dict.get(‘a’)
items()dict.items()Returns a view object that displays a list of dictionary’s (key, value) tuple pairs.my_dict = {‘a’: 1, ‘b’: 2}<br>items = my_dict.items()
keys()dict.keys()Returns a view object that displays a list of all the keys in the dictionary.my_dict = {‘a’: 1, ‘b’: 2}<br>keys = my_dict.keys()
values()dict.values()Returns a view object that displays a list of all the values in the dictionary.my_dict = {‘a’: 1, ‘b’: 2}<br>values = my_dict.values()
pop()dict.pop(key[, default])Removes the item with the specified key and returns its value. If the key is not found, returns default (or raises KeyError if not provided).my_dict = {‘a’: 1, ‘b’: 2}<br>value = my_dict.pop(‘a’)
popitem()dict.popitem()Removes and returns an arbitrary (key, value) pair from the dictionary.my_dict = {‘a’: 1, ‘b’: 2}<br>item = my_dict.popitem()
update()dict.update([other])Updates the dictionary with elements from another dictionary or from an iterable of (key, value) pairs.my_dict = {‘a’: 1, ‘b’: 2}<br>my_dict.update({‘c’: 3})
fromkeys()dict.fromkeys(seq[, value])Creates a new dictionary with keys from the given sequence and values set to a default value (None by default).keys = [‘a’, ‘b’, ‘c’]<br>my_dict = dict.fromkeys(keys)

Written by HintsToday Team

Related Posts

Python input function in Detail- interesting usecases

The input() function in Python is primarily used to take input from the user through the command line. While its most common use is to receive text input, it can be used creatively for various purposes. Here are some interesting uses of the input() function along with...

read more

Python Strings Interview Questions

Python Programming Strings Interview Questions Write a Python program to remove a Specific character from string? Here's a Python program to remove a specific character from a string: def remove_char(text, char): """ Removes a specific character from a string. Args:...

read more

Get the latest news

Subscribe to our Newsletter


Submit a Comment

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