Python Dictionary
Python Tutorial » Python Dictionary
Python dictionary is an unordered collection of elements. Each element of a dictionary has a key-value pair.
Example: Dictionaries are written using curly brackets, and it have keys and values.
Output:
{1: 'John', 2: 'Chris', 3: 'Tom', 4: 'Nick'}
my_dictionar = {1: 'John', 2: 'Chris', 3: 'Tom', 4: 'Nick'}
print(my_dictionar)
print(my_dictionar)
Output:
{1: 'John', 2: 'Chris', 3: 'Tom', 4: 'Nick'}
Python Dictionaries NOT Allow Duplicates - examples
Example: Dictionaries cannot have two items with the same key
{1: 'John', 2: 'Chris', 3: 'Tom', 4: 'Nicole'}
my_dictionar = {
1: 'John',
2: 'Chris',
3: 'Tom',
4: 'Nick',
4: 'Nicole' }
print(my_dictionar)
Output: 1: 'John',
2: 'Chris',
3: 'Tom',
4: 'Nick',
4: 'Nicole' }
print(my_dictionar)
{1: 'John', 2: 'Chris', 3: 'Tom', 4: 'Nicole'}
How to Get the Length of a Dictionary
Example: how many elements this Dictionary has, using len() function
4
my_dictionar = {
1: 'John',
2: 'Chris',
3: 'Tom',
4: 'Nick',
4: 'Nicole' }
print(len(my_dictionar))
Output:1: 'John',
2: 'Chris',
3: 'Tom',
4: 'Nick',
4: 'Nicole' }
print(len(my_dictionar))
4
Dictionary Items - Data Types
The values in dictionary elements can be any data type:
Example: elements can be any data type:
{'country': 'USA', 'make': 'Ford', 'electric': True, 'year': 1902, 'colors': ['black', 'white', 'blue']}
my_dictionar = {
"country": "USA",
"make": "Ford",
"electric": True,
"year": 1902,
"colors": ["black", "white", "blue"]
}
print(my_dictionar)
Output:"country": "USA",
"make": "Ford",
"electric": True,
"year": 1902,
"colors": ["black", "white", "blue"]
}
print(my_dictionar)
{'country': 'USA', 'make': 'Ford', 'electric': True, 'year': 1902, 'colors': ['black', 'white', 'blue']}
Python Sets methods, difference, vs lists, pop, operation, update, documentation, index, intersection, are mutable or immutable
Python Dictionary - python
Online Editor
This tool makes it easy to create, adjust, and experiment with custom colors for the web.
HTML Templates

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.
CSS HTML Layout

Find here examples of creative and unique website layouts.
Free CSS HTML Menu

Find here examples of creative and unique website CSS HTML menu.