omics.co.in

Lists

Lists

In Python, a list is a data structure that can store a collection of values. Lists are created using square brackets, []. The values in a list can be of any type, including integers, floats, strings, and other lists.

 

Suppose you are working with a set of DNA sequences and you want to determine the length of each sequence. You can store the sequences as a list of strings, and then use a loop to iterate through the list and calculate the length of each string. Here’s an example code snippet:

 

# List of DNA sequences

Pyton
				DNA_sequences = ['ATCGATTAG', 'ATAGCTGCA', 'CGTAAGCCT']
#To see the contents in the list
print(DNA_sequences)
			

This will print as output

[‘ATCGATTAG’, ‘ATAGCTGCA’, ‘CGTAAGCCT’]

Lists can be modified by adding, removing, replacing or deleting values. For example, the following code adds a sequence “AAATTTCCC” to the DNA sequences list:

Pyton
				DNA_sequences.append("AAATTTCCC")
print(DNA_sequences)

			

this will print as output

[‘ATCGATTAG’, ‘ATAGCTGCA’, ‘CGTAAGCCT’, ‘AAATTTCCC’]

 

The following code removes the name “CGTAAGCCT” from the DNA_sequences list:

Pyton
				DNA_sequences.remove("CGTAAGCCT")
print(DNA_sequences)

			

this will print as output

[‘ATCGATTAG’, ‘ATAGCTGCA’, ‘AAATTTCCC’]

 

The following code replaces the sequence “ATCGATTAG” with the sequence “GGGCCCGGG”:

Pyton
				DNA_sequences[0] = ("GGGCCCGGG")
print(DNA_sequences)

			

this will print as output

[‘GGGCCCGGG’, ‘ATAGCTGCA’, ‘AAATTTCCC’]

 

Note: Index of an element -To access a specific element in a sequence, you can use its position or index. In Python, indexing starts at 0, meaning that the first element in a sequence has an index of 0, the second element has an index of 1, and so on. You can use square brackets [] to access the element at a specific position in a sequence.

 

To delete a sequence from a Python list, you can use the del statement followed by the index of the sequence you want to remove. For example, suppose you have a list of DNA sequences:

 

To delete 2nd sequence in this list

 

[‘GGGCCCGGG’, ‘ATAGCTGCA’, ‘AAATTTCCC’]

Pyton
				del DNA_sequences[1]
print(DNA_sequences)

			

this will print

[‘GGGCCCGGG’, ‘AAATTTCCC’]

 

Here are some other lists for practise

Pyton
				plants = ["Rose", "Tulip", "Lily", "Tomato"]
			
Pyton
				# A list of bacteria
bacteria = ["Escherichia coli", "Staphylococcus aureus", "Mycobacterium tuberculosis", "Klebsiella pneumoniae", "Pseudomonas aeruginosa"]

			

Note: Comments in Python. The # symbol is used to add comments to your code. Comments are lines of text that are ignored by the Python interpreter and are used to add notes or explanations to your code. Comments can be placed anywhere in your code and are indicated by the # symbol at the beginning of the line.

 

For example, consider the following Python code:

Pyton
				# This is a comment
print("Hello, world!")

			

In this code, the first line is a comment, and it is ignored by the Python interpreter. The second line is a print statement that will output “Hello, world!” to the console when the code is run.

 

Comments are useful for documenting your code, making notes to yourself or others who may be reading your code, and for temporarily disabling code that you may not want to run. By using comments effectively, you can make your code more understandable, maintainable, and easier to debug.

← Return to Home Hub Scroll to Top ↑