Description

What is Dictionary?

Dictionaries are used to store data values in Key: Value pairs. A dictionary is a collection which is ordered, changeable, and do not allow duplicates.

Dictionay is written with curly brackets {}.

For Example:

Syntax:

d = {
    "Key": "Value",
}
city = {
    "London": "Capital of England",
    "Dubai": "City in the United Arab Emirates",
    "Los Angeles": "City in California",
    "Houston": "Texas"
}
print(city)

Output:

{'London': 'Capital of England', 'Dubai': 'City in the United Arab Emirates', 'Los Angeles': 'City in California', 'Houston': 'Texas'}

Access Element From Dictionary in Python.

city = {
    "London": "Capital of England",
    "Dubai": "City in the United Arab Emirates",
    "Los Angeles": "City in California",
    "Houston": "Texas"
}
print(city["Dubai"])

Access  Element from a Dictionary with get() method.

city = {
    "London": "Capital of England",
    "Dubai": "City in the United Arab Emirates",
    "Los Angeles": "City in California",
    "Houston": "Texas"
}
print(city.get("Dubai"))

 

Output:

City in the United Arab Emirates

Add Dictionary Element (Key Value pair)

city = {
    "London": "Capital of England",
    "Dubai": "City in the United Arab Emirates",
    "Los Angeles": "City in California",
    "Houston": "Texas"
}
city["San Francisco"] = "California"
print(city)

Add Element in Dictionary with update() method.

city = {
    "London": "Capital of England",
    "Dubai": "City in the United Arab Emirates",
    "Los Angeles": "City in California",
    "Houston": "Texas"
}
city.update({"San Francisco": "California"})
print(city)

Output:

{'London': 'Capital of England', 'Dubai': 'City in the United Arab Emirates', 'Los Angeles': 'City in California', 'Houston': 'Texas', 'San Francisco': 'California'}

Delete/Remove Element in Dictionary.

city = {
    "London": "Capital of England",
    "Dubai": "City in the United Arab Emirates",
    "Los Angeles": "City in California",
    "Houston": "Texas"
}
del city["Houston"]
print(city)

Output:

{'London': 'Capital of England', 'Dubai': 'City in the United Arab Emirates', 'Los Angeles': 'City in California'}

Copy Dictionary with copy() method.

city = {
    "London": "Capital of England",
    "Dubai": "City in the United Arab Emirates",
    "Los Angeles": "City in California",
    "Houston": "Texas"
}
city1 = city.copy()
print(city1)

Output:

{'London': 'Capital of England', 'Dubai': 'City in the United Arab Emirates', 'Los Angeles': 'City in California', 'Houston': 'Texas'}

Methods of Dictionary in Python Programming language.

Methods Description
copy() copy() method returns a shallow copy of the dictionary.
clear() clear() method removes all items from the dictionary.
pop() Removes and returns an element from a dictiionary having the given key.
popitem() Removes the arbitrary key-value pair from the dictionary and returns it as tuple.
get() It is a conventional method to access the a value for a key.
dictionary_name.values() returns a list of all the values available in a given dictionary.
str() Produces a printable string representation of a dictionary.
update() Add dictionary key value pair to dictionary.
setdefault()

Set dict[key] = default id key is not already in dict.

keys()

Returns list of dictionary ditc's keys.

items() Returns a list of dict's (key, value) tuple pairs
has_key() Returns true if key in dictionary dict, false otherwise.
fromkeys() Create a new dictionary with keys from seq and values set to value.
type() Returns the type of passed variable.
cmp() Compares elements of both dict.

 

Comments

Login is mandatory to comment Please login

Recommended Posts

Implement of All Method of String in Python Programming language

Implementation of all methods of string in python. How to implement of method on string.

Operators based Exercise 1 in Python Programming language.

All operators used for better understanding the python operators.

Sets in Python | Perform Set methods

A Set is an unordered collection data type which does not contain duplicates elements. A set is iterable and mutable data type.

Conditional Statement If elif else statement in Python

Like some situation comes in our real life in which we have to take some decision as to what we have to do next.

Everything about of Loops in Python | For loop | While loop | Nested Loop

A loop is used for iterate the sequence. It is used to iterate List, String, Dictionary, Tuple. Iterating over a sequence is called traversal.

Function | Docstring | Lambda | Kwargs in Python

In Python programming, a function is a block of statements that makes our code readable. If we do that function in the program, then we do not have to write the same statement of code again and again.