Lesson 4 - Operations on lists

  • Modifying a list item

    The value of a specific list item can be modified using the index number.

    fruits = ["apple", "banana", "kiwi", "orange", "grape", "cherry"]
    For example, to change kiwi to melon
    fruits[2] = "melon"
    print(fruits)  outputs ["apple", "banana", "melon", "orange"]

    Change a range of list values

    If you want to modify series of values, you can do it by using the range index

    fruits = ["apple", "banana", "kiwi", "orange", "grape", "cherry"]
    For example, to change 3 values in the fruits list i.e banana, kiwi, orange to melon, berry, peach
    fruits[1:4] = ["melon", "berry", "peach"]
    print(fruits)  outputs ["apple", "melon", "berry", "peach", "grape", "cherry" ]

    In case you want to replace series of values with a single value, then you can do it the following way
    fruits = ["apple", "banana", "kiwi", "orange", "grape", "cherry"]
    fruits[1:4] = ["melon"]
    print(fruits) outputs ["apple", "melon", "grape", "cherry" ]


    List methods

    The following built-in methods can be used to perform various operations on Lists in Python


    Using append() method

    You can append an item to the end of the list

    fruits = ["apple", "kiwi", "cherry"]
    fruits. append("orange")

    print(fruits)  outputs ["apple", "kiwi", "cherry", "orange"]

    Using clear() method

    The clear() method empties the list. 

    print(fruits) outputs []
    The list remains, but the content is removed

    Using copy() method

    list copying simply with an assignment operator i.e  list2 = list1 does not work, list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2.

    copy() method allows copy of a list

    fruits1 = ["apple", "kiwi", "cherry"]
    fruits2 = fruits1.copy()
    print(fruits2) outputs ["apple", "kiwi", "cherry"]





    Using count() method

    It counts the number of times a value appears in the list

    fruits = [“orange”,”apple”,”kiwi”,”banana”,”grape”, “apple”]
    print(fruits.count(“apple”)) outputs 2

    Using extend() method

    You can extend a list with another list

    list1 = ["Chicago","New York", "New Jersy"]
    list2 = ["Dallas", "Austin"]
    print(list1) outputs ['Chicago', 'New York', 'New Jersy', 'Dallas', 'Austin']

    Using index() method

    You can get index of a given element in the list

    cities = ['Chicago', 'New York', 'New Jersy', 'Dallas', 'Austin']
    cities.index('Dallas') returns 3



    Using insert() method

    You can insert a value in the list at a given index position
    cities = ['Chicago', 'New York', 'New Jersy', 'Dallas', 'Austin']
    cities.insert(3, "Denver")
    print(cities) outputs ['Chicago', 'New York', 'New Jersy', 'Denver', 'Dallas', 'Austin']

    Using pop() method

    The pop() method returns the last element in the list by default. If you specify index, then it returns the corresponding element

    cities.pop() returns 'Austin'
    cities.pop(2) returns 'New Jersy'

    Using remove() method

    With remove method, you can remove a value in the list

    cities = ['Chicago', 'New York', 'New Jersy', 'Dallas', 'Austin']
    print(cities)  outputs ['New York', 'New Jersy', 'Denver', 'Dallas', 'Austin']

    Using reverse() method

    The items in the list are reversed with this method

    list1 = [1, 2, 3, 5, 1, 2, 6]
    print(list1) outputs [6, 2, 1, 5, 3, 2, 1]


    Using sort() method

    sorts the list elements in ascending order.

    cities = [‘Chicago’, ‘New York’, ‘New Jersy’, ‘Dallas’, ‘Austin’]
    [‘Austin’, ‘Chicago’, ‘Dallas’, ‘New Jersy’, ‘New York’]

    [‘New York’, ‘New Jersy’, ‘Denver’, ‘Dallas’, ‘Austin’]


    Sort the lists ( Ascending)

    You can sort the elements of a list using sort() function
    thislist = [90, 50, 165, 84, 23]

    The above sort() method sorts the elements in ascending order

    Sort the lists ( Descending)

    To sort in descending order
    thislist = [90, 50, 165, 84, 23]

    Reverse order

    You can reverse the order of items in a list using reverse() function

    fruits = [“apple”, “banana”, “kiwi”, “orange”, “grape”, “cherry”]
    [‘cherry’, ‘grape’, ‘orange’, ‘kiwi’, ‘banana’, ‘apple’]