1 of 22

List, Dictionary and Tuple

2 of 22

List

  • A storage for saving numerous elements. List can be considered as the collection of multiple variables.
  • How to define a list
    • The list is enclosed by [ ].
    • The elements of the list is separated by ,
    • The index system of the elements is started from 0.

List_a = [123, 456, 789, 10, 0, 454]

List name

elements

Values

123

456

789

10

0

454

Index

0

1

2

3

4

5

3 of 22

Basic calling of a list

  • Calling by the index
  • Calling by the reverse index

Values

Lakers

Bulls

Warriors

Nets

Spurs

Forward index

0

1

2

3

4

Reverse index

-5

-4

-3

-2

-1

4 of 22

Basic operations of a list

  • Duplicates the elements of a list
  • Splice a list to be a sub-list

Genes[1:5:2]

Start index

length

interval

genes

dnaA

NfkB

HER2

ENG

p53

[2:5]

[1:-1]

[0:4]

[:4]

[1:5:2]

5 of 22

Basic operations of a list

  • Delete the elements of a list
  • Generate a subset from a list
  • Only a list can add to another one

6 of 22

Insert elements into a list

  • Replace the values of the elements in a list
  • Insert a new element into a list – our intuition
  • Insert a new element into a list – append, extend, insert

insertion

append

Add element to the end of list.

extend

Add a list to end of another one

insert

Insert a element to a specific index

index

7 of 22

Remove the elements from a list

  • pop() – return the last element and remove it from the list
  • list.remove() – remove the first matching element from a list

8 of 22

Useful functions for a list

  • Get the index of the first matching element in a list.
  • Sort the list
  • Reverse the list

9 of 22

Useful functions for a list

  • Get the number of elements in a list
  • Get the maximum or minimum value in a list
  • Count the number of a specific elements in a list

10 of 22

Useful functions for a list

  • Generate a list with continuous sequence of values
  • Print a list as a string

11 of 22

Exercise

  • We have a list of genes with 2 mutated ones, could you please help us to get the genes which have mutations?

mutation

Gene1

No

Gene2

No

Gene3

Yes

Gene4

No

Gene5

Yes

Gene6

No

Gene7

No

Gene8

No

We can do it better with if…else statement and loop

12 of 22

Manage a string

  • List-like operations
  • Reverse the string
  • Replace a word in a string with another word

13 of 22

Manage a string

  • Make string to be a list
  • Capital insensitive
  • Search the first matching word of a string

14 of 22

Manage a string

  • Forward/backward match

This function is extremely useful to deal with fasta sequence

15 of 22

Exercise

  • A researcher discovered a very important gene which can heal lung cancer. But he made a mistake to show the gene sequence from 3’ to 5’. Moreover, he mixed the capital and small alphabets. Could you please help him to get the right one (from 5’ to 3’)?

16 of 22

Tuple

  • Tuple is a list which cannot be modified. Thus, it can be processed faster and also good for storing important information.
  • How to define tuple:
    • The list is enclosed by ().
    • The elements of the list is separated by ,
    • The index system of the elements is started from 0.

17 of 22

Convert between list and tuple

18 of 22

Dictionary (dict)

  • Dictionary (dict) can store any types of variables including list, tuple and dictionary with corresponding unique keys. Thus, it is easy for searching and modification. But it takes more memory than list.
  • How to define dictionary:
    • The list is enclosed by {}.
    • An element is composed of a key and a value. The key and value are separated by :
    • The elements of the dictionary is separated by ,
    • A dict can used a key to call its corresponding value via [].

19 of 22

Manage a dictionary

  • Reassign and add elements in a dict
  • Deletion

20 of 22

Manage a dictionary

  • Extract the key information
  • Extract the value information
  • Extract the key and value at the same time

21 of 22

Exercise

  • We have 4 patients who were infected by an unknown bacteria. Today, we received a great news, the 4th patient was recovered. So we may be able to get the plasma from him for further research. Could you please give me the patient’s information?

Name

Age

Gender

Duration

Syndromes

John

33

Male

7

cough, fever, red skin

Lucas

23

Male

2

cough, fever, vomit

Amy

60

Female

12

cough, fever, vomit, headache

Bill

42

Male

13

cough, fever, headache

22 of 22