Skip to content

Day 4: Lists

1. List

Till now we are using list to store data and plot them. Now today we will learn about it extensively. List is a data structure, means a way to store and access data. So today we will learn how to store and access data in list data structure.

  • a list is a collection of data in a finite sequence.
  • it can hold values of multiple datatype, i.e., one entry can be integer, other can float or string.

1.1 Creating lists

A list is created using a square bracket as follows,

a = [] # an empty list
b = [1,2,3,4,5] # list of numbers
c = ["sandeep", "suman", 2020, 7] # mix datatypes
d = [["sandeep", "suman"], 2020, []] # list inside list

print(c)
['sandeep', 'suman', 2020, 7]

1.2 Accessing data inside list

Each entry has an index or position in a list, which can be used to access them. As counting starts in python from 0, So the first index is 0 and second index is 1 and so on.

c = ['sandeep', 'suman', 2020, 7]
print(c[0]) # to get the 0th element (count with 0)
print(c[2]) # to get the 2nd element (count with 0)
print(c[-1]) # count from last
sandeep
2020
7

1.3 Basic list operations

  • Length: The length of a list can be computed as follows

    len(c)
    
    4
    
  • Addition: Two or more lists can be added with + operation as follows

    a = [1, 2]
    b = ["a", "b"]
    c = a + b
    print(c)
    
    [1, 2, 'a', 'b']
    
  • Multiplication: Repetition can be achieved with * operation as follows

    a = ['Hi'] * 5
    print(a)
    
    ['Hi', 'Hi', 'Hi', 'Hi', 'Hi']
    
  • Slicing: A part of of list starting with i index and end before j can be obtained by List[i:j], for example

    c = ["sandeep", "suman", 2020, 7]
    print(c[1:3])
    
    ['suman', 2020]
    

1.4 List as Stack

Stack is a data structure based on LIFO(Last in first out).

  • Append: The append method on a list will add element in the end of the list.

    L = []
    L.append(1) # 1 will be added to L
    L.append(2) # 2 will be added to L
    print(L)
    
    [1, 2]
    
  • Pop: The pop method on list will remove the element from the last.

    L = [1, 2]
    L.pop()
    print(L)
    
    [1]
    

    The method pop also takes addition argument to remove element from any given index.

    L = [1, 2]
    L.pop(0) # removes the element with index 0
    print(L)
    
    [2]
    

1.5 Other method on list

  • Index: This method is used to get the index of an element.

    L = ["apple", "mango", "orange"]
    i = L.index("mango")
    print(i)
    
    1
    
  • Remove: This method is used to remove an element in the list.

    L = ["apple", "mango", "orange"]
    L.remove("mango")
    print(L)
    
    ['apple', 'orange']
    
  • Reverse: This method will reverse the elements of the given list.

    L = ["apple", "mango", "orange"]
    L.reverse()
    print(L)
    
    ['orange', 'mango', 'apple']
    
  • Sort: This method is used to sort the elements of a list.

    L = [4, 6, 2, 7, 1, 3]
    L.sort()
    print(L)
    
    [1, 2, 3, 4, 6, 7]
    

Tip

The maximum element of a list can be obtained using sort taking in the last element. i.e.,

L = [4, 6, 2, 7, 1, 3]
L.sort()
max = L[-1]
print(max)
7

1.6 Membership and Iterating of a List

  • Like the membership in the set. We can tell if something is inside a list or not as follows

    L = ["apple", "mango", "orange"]
    print("apple" in L)
    print("guava" in L)
    
    True
    False
    
  • Often in programming we want look for each element of a list one by one from start to finish. The process is generally known as iteration in programming.

    A simple program to iterate a list is as follows

    fruits = ["apple", "mango", "orange"]
    for i in fruits: # semicolon is used to make code block
         print(i)    # 4 space is used here !important
    
    apple
    mango
    orange
    

2. References

  1. W3School on List
Back to top