Python Lists- The Basics

Posted by Anoop Nair on November 14, 2017 Tags: Python

In this post we will explore the basics of python lists. A python list is a datatype available in python which can be written as comma seperated values between square brackets.

    
    list1 = [1,2,3,4] #list of numbers.
    list2 = ["John", "Henry", "Hellen"] # list of strings.
    

But thats not all the best part is we can have different datatypes in a single list, we can even have another list in an existing list.

    
    #Name and their respective weights.
    Weight = ["Henry", 64, "Dustin", 58, "Nancy", 50] 

    #List inside a Height List
    Height= [["Henry", 1.74],
            ["Dustin", 1.56],
            ["Nancy", 1.60]]
    

Accessing values of the list

Every element in a list have a index no. and it starts with 0. So to print a particular element of the list we can simply pass the name of the list followed by the index no. inside the square bracket Height[2], to the print function.

We can use negative sign to get values starting from the end of the list. Following code will explain it better.

    
    Weight = ["Henry", 64, "Dustin", 58, "Nancy", 50]

    #print the weight of Henry

    print(Weight[1])  # output  = 64

    # print the last element of the Weight list

    print(Weight[-1])  # output = 50
    

In a situation like the Height list that we created earlier, a full list is a single element i.e ["Henry", 1.74 ] is a single element with index value 0. So for example if we want access the height of Nancy we will need two square brackets like Height[2][1], here the first square bracket has the index value of sub-list( with Nancy's name and height) with respect to the Height list, and the second bracket contains the index value of Nancy's height i.e index value of 1.

    
    Height= [["Henry", 1.74],
            ["Dustin", 1.56],
            ["Nancy", 1.60]]

    #print Dustin's Height

    print(Height[1][1])  # output = 1.56

    #print Nancy's Height

    print(Height[2][1])  # output = 1.6
    

Slicing And Dicing A List

What if I only want to access a range of indixes i.e elementls between index 1 and 5. For that we can use the following method, instead of specifying just a single index in square bracket we will specifiy the starting and ending indexes seperated by a colon Weight[1:4] . An important point to note is that value at starting index will be included while value at ending index will be excluded.

There are two important points that you should note:
  • If you leave the starting index empty Weight[ :4 ] then all the result will be all the elements starting from index 0 to 3.
  • If you leave the ending index empty Weight[ 2: ] then all the result will be all the elements starting from the starting index i.e 2 to the end of the list including the last element.
  • We can even use the negative sign to select a index starting from the end of the list Weight[2:-2]

    
        Weight = ["Henry", 64, "Dustin", 58, "Nancy", 50, "Kevin", 67, "Sara", 55]

        print( Weight[1:5] )
        # output = [64, 'Dustin', 58, 'Nancy']

        print( Weight[ :5] )
        #output = ['Henry', 64, 'Dustin', 58, 'Nancy']

        print( Weight[1: ] )
        #output = [64, 'Dustin', 58, 'Nancy', 50, 'Kevin', 67, 'Sara', 55]

        # you can even use the negative sign to select a index starting from the end of the list 
        print( Weight[2:-2])
        #output = ['Dustin', 58, 'Nancy', 50, 'Kevin', 67]
    
    

Follow the following links to see learn more about python lists Python- List Manipulation.


COMMENTS