omics.co.in

Dictionaries

Dictionaries

A dictionary is a data structure that allows you to store key-value pairs. Each key in the dictionary maps to a specific value, which can be any data type, including strings, integers, lists, or even other dictionaries. Dictionaries are particularly useful in bioinformatics applications where you need to store and retrieve information based on a specific identifier or key, such as gene names or protein IDs.

 

To create a dictionary in Python, you can use curly braces {} or the dict() function. For example, suppose you want to create a dictionary that maps each nucleotide base to its corresponding full name:

Pyton
				base_names = {'A': 'Adenine', 'T': 'Thymine', 'G': 'Guanine', 'C': 'Cytosine'}
print(base_names)

			

This will output:

{‘A’: ‘Adenine’, ‘T’: ‘Thymine’, ‘G’: ‘Guanine’, ‘C’: ‘Cytosine’}

 

Here, each base (A, T, G, and C) is a key in the dictionary, and each full name (Adenine, Thymine, Guanine, and Cytosine) is the corresponding value. You can access the value of a specific key by using square brackets [] and the key itself:

Pyton
				print(base_names['A'])
			

This will output:

Adenine

 

You can also modify the value associated with a key by using the same syntax:

Pyton
				base_names['T'] = 'Uracil'

#This will change the value associated with the key 'T' from 'Thymine' to 'Uracil'.

print(base_names)
			

This will output:

{‘A’: ‘Adenine’, ‘T’: ‘Uracil’, ‘G’: ‘Guanine’, ‘C’: ‘Cytosine’}

 

 

For ease Dictionary can also be written as

Pyton
				my_dict = {
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}

			

Another example of Dictionary

Pyton
				scientific_name = {"Human": "Homo sapiens", "Elephant" : "Elephas maximus", "Tomato": "Solanum lycopersicum"}
			

for ease it can also be written as

Pyton
				scientific_name = {
                    "Human": "Homo sapiens",
                    "Elephant": "Elephas maximus",
                    "Tomato": "Solanum lycopersicum"
                    }

			

In Python Two elements can be mixed together.

 

List of Dictionaries

 

list of animals with the following information:

Name

Species

Habitat

Diet

 

Here is an example of how to create a list of animals with this information:

Pyton
				# Create a list of animals
animals = [
  {
    "name": "Lion",
    "class": "Mammalia",
    "habitat": "Africa",
    "diet": "Carnivore"
  },
  {
    "name": "Tiger",
    "class": "Mammalia",
    "habitat": "Asia",
    "diet": "Carnivore"
  },
  {
    "name": "Elephant",
    "class": "Mammalia",
    "habitat": "Africa",
    "diet": "Herbivore"
  }
]

# Print the list of animals
print(animals)

			

This would print the following output:

 

[{‘name’: ‘Lion’, ‘species’: ‘Mammal’, ‘habitat’: ‘Africa’, ‘diet’: ‘Carnivore’}, {‘name’: ‘Tiger’, ‘species’: ‘Mammal’, ‘habitat’: ‘Asia’, ‘diet’: ‘Carnivore’}, {‘name’: ‘Elephant’, ‘species’: ‘Mammal’, ‘habitat’: ‘Africa’, ‘diet’: ‘Herbivore’}]

Data manipulation in dictionaries

Dictionaries are a powerful data structure in Python that allows you to store key-value pairs. One of the key benefits of dictionaries is that they can be easily manipulated to add, remove, or modify elements.

 

Here are some common data manipulation techniques that can be performed on dictionaries in Python:

 

Adding elements to a dictionary:

You can add new elements to a dictionary by assigning a value to a new key, like this:

Pyton
				# Create an empty dictionary
my_dict = {}

# Add a new key-value pair to the dictionary
my_dict["apple"] = 3

			

In this example, we create an empty dictionary called my_dict, and then add a new key-value pair to it with the key “apple” and the value 3.

 

Removing elements from a dictionary:

You can remove elements from a dictionary using the del statement or the pop() method. Here is an example using del:

Pyton
				# Create a dictionary
my_dict = {"apple": 3, "banana": 2, "orange": 1}

# Remove an element from the dictionary
del my_dict["banana"]

			

In this example, we remove the element with the key “banana” from the dictionary using the del statement.

 

Modifying elements in a dictionary:

You can modify elements in a dictionary by re-assigning a new value to an existing key, like this:

Pyton
				# Modify an element in the dictionary
my_dict["orange"] = 4

			

In this example, we modify the value of the key “orange” to 4.

 

Accessing elements in a dictionary:

You can access elements in a dictionary using their keys, like this:

Pyton
				# Create a dictionary
my_dict = {"apple": 3, "banana": 2, "orange": 4}

# Access an element in the dictionary
print(my_dict["apple"])

			

In this example, we access the value of the key “apple” and print 3 to the console.

 

These are just a few examples of the many data manipulation techniques that can be performed on dictionaries in Python. With dictionaries, you can easily store and manipulate data in key-value pairs, making them a powerful tool for data analysis and manipulation in many different applications.

← Return to Home Hub Scroll to Top ↑