- Views: 1
- Report Article
- Articles
- Writing
- Article Marketing
Python Dictionaries
Posted: Jun 18, 2022
A dictionary is a series of pairs of things. It is a collection which is unordered, changeable and indexed.
Each pair contains a key—"firstname", "last name" etc.—and a value—"Mansoor", "Ahmed", etc
To pick something out of a dictionary, we specify a particular key and
ask what value is paired with it
Dictionaries are written with curly brackets, and they have keys and values.
In a dictionary, each chunk is a paired key and value.
Notice that the key is followed by a colon:
the variable name is singular, not plural
All the values are strings, enclosed in quotation marks, and so are all the keys
Dictionary’s purpose is to store information that we can later lay our hands on.
For example, if someone wants to know the address of "Mansoor Ahmed" how does he will find it?
In a dictionary, we pick out an element by specifying its key:
address_of_customer = customer_12345 ["address"]
print (address_of_customer)
The keys can be strings and numbers.
To pick out a value, we use the number
Values can be numbers, too :
To pick out a value, we use the key, a string in this case
We can mix strings and numbers any way we want
How to add a key-value pair to a dictionary
Let us see we have dictionary
customer_12345 = {"first name": "Mansoor", "last name": "Ahmed", "address": "H.No.3 Ibrahim Fibres Ltd Colony"}
We can add a new pair by writing
customer_12345["city"]= "Faisalabad"
We can define an empty dictionary, a dictionary with no key-value pairs:
things_to_remember = {}
Later, we can fill the dictionary with pairs, adding one at a time
things_to_remember[1000] ="highest number"
thing_to_remember["last name"]= "Ahmed"
How to delete an element from dictionary:
del customer_12345["address"]
It begins with the same keyword:
Then comes the name of the dictionary.
And the particular piece of information we're after is specified by the key (in the dictionary), in square brackets
Then the assignment of the new value.
Looping through a values
- customer_12345 = {"first name": "Mansoor", "last name": "Ahmed", "address": "H.No.3 Ibrahim Fibres Ltd Colony"}
- for each_value in customer_12345.value():
- print (each_valuve)
The loop begins with the familiar for:
Next comes a variable to store the value for each iteration.
Next, the keyword in followed by the name of the dictionary, customer_12345
Then a dot…
Then the keyword values…
then empty parentheses and colon
Looping through keys
This is a dictionary
customer_12345 = {"first name": "Mansoor", "last name": "Ahmed", "address": "H.No.3 Ibrahim Fibres Ltd Colony"}
This is the loop:
for each_key in customer_12345.keys():
print(each_key)
Python displays:
first name
last name
address
Looping through key-value pairs
This is the dictionary we've been working with:
customer_12345 = {"first name": "Mansoor", "last name": "Ahmed", "address": "H.No.3 Ibrahim Fibres Ltd Colony"}
Here's the code for looping through the dictionary and printing all the keys and values:
for each_key,each_value in customer_12345.items():
print("The_customer’s" + each_key + " is" + each_value)
Following the instructions above, Python displays:
The customer's first name is Mansoor
The customer's last name is Ahmed
The customer's address is H.No.3 Ibrahim Fibres Ltd Colony
Length of Dictionary
To determine how many items (key-value pairs) a dictionary has, use the len () function.
Example: Print the number of items in the dictionary:
print(len(thisdict))
How to append a new dictionary to a list of dictionaries
To learn how many dictionaries are in the list, we measure the list's length using the keyword len, for length, we write
new_customer_id = len(customers)
We can create the new dictionary
new_dictionary = {"customer id": new_customer_id,"first name new_first_name, "last name": new_last_name,"address": new_address,}
Finally, we append this new dictionary to the list:
customers.append(new_dictionary)
Mansoor Ahmed Chemical Engineer,Web developer