Python Dictionary: Accessing The Dictionary Values
Python Dictionary: Accessing The Dictionary Values
Dictionary is used to implement the key-value pair in python. The dictionary is the data
type in python which can simulate the real-life data arrangement where some specific
value exists for some particular key.
In other words, we can say that a dictionary is the collection of key-value pairs where
the value can be any python object whereas the keys are the immutable python object,
i.e., Numbers, string or tuple.
However, the values can be accessed in the dictionary by using the keys as keys are
unique in the dictionary.
Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}
print(type(Employee))
print("printing Employee data .... ")
print("Name : %s" %Employee["Name"])
print("Age : %d" %Employee["Age"])
print("Salary : %d" %Employee["salary"])
print("Company : %s" %Employee["Company"])
Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}
print(type(Employee))
print("printing Employee data .... ")
print(Employee)
print("Enter the details of the new employee....");
Employee["Name"] = input("Name: ");
Employee["Age"] = int(input("Age: "));
Employee["salary"] = int(input("Salary: "));
Employee["Company"] = input("Company:");
print("printing the new data");
print(Employee)
Deleting elements using del keyword
The items of the dictionary can be deleted by using the del keyword as given below.
Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}
print(type(Employee))
print("printing Employee data .... ")
print(Employee)
print("Deleting some of the employee data")
del Employee["Name"]
del Employee["Company"]
print("printing the modified information ")
print(Employee)
print("Deleting the dictionary: Employee");
del Employee
print("Lets try to print it again ");
print(Employee)
Iterating Dictionary
A dictionary can be iterated using the for loop as given below.
Example 1
# for loop to print all the keys of a dictionary
Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}
for x in Employee:
print(x);
Output:
Name
Company
salary
Age
Example 2
#for loop to print all the values of the dictionary
Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}
for x in Employee:
print(Employee[x]);
Output:
29
GOOGLE
John
25000
Example 3
#for loop to print the values of the dictionary by using values() method.
Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}
for x in Employee.values():
print(x);
Output:
GOOGLE
25000
John
29
Example 4
#for loop to print the items of the dictionary by using items() method.
Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}
for x in Employee.items():
print(x);
Output:
('Name', 'John')
('Age', 29)
('salary', 25000)
('Company', 'GOOGLE')
Employee = {"Name": "John", "Age": 29, "Salary":25000,"Company":"GOOGLE","Name"
:"Johnn"}
for x,y in Employee.items():
print(x,y)
1. Output:
2. Salary 25000
3. Company GOOGLE
4. Name Johnn
5. Age 29