Python |
||
Python - Dictionary
Like a Set or List in Python, a Dictionary is a of distict elements. The main difference between a Dictionary and List/Array is that it assigns a specific key for every elements in it. Every elements in a dictionary is specified as {'Key' : Element }.
A Python dictionary is a built-in data structure that stores a collection of key-value pairs. Dictionaries are mutable, unordered, and allow for efficient retrieval, insertion, and deletion of elements based on their keys. The keys in a dictionary must be unique and hashable, which means they can be of data types like integers, floats, strings, and tuples (if the tuple contains hashable elements). The values associated with the keys can be of any data type, including other dictionaries or lists.
To create a dictionary, you can use curly braces {} and separate the keys and values with colons, while key-value pairs are separated by commas. An empty dictionary can be created using an empty pair of curly braces {} or the dict() constructor.
Some of the main features of dictionaries include:
Python provides various built-in methods and functions to manipulate dictionaries, such as:
Differences among Array, Set, Dictionary
Arrays, sets, and dictionaries are all collection data structures in Python, but they have some key differences in terms of their properties and use cases. Here's a summary of their main differences:
Arrays:
Sets:
Dictionaries:
Examples
Followings are example showing various usages and operation for Dictionary
NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x NOTE 2 : All the examples in this page are assumed to be written/run on Windows 7 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.
|
||