Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Introduction to Data Analysis

with Python and R


(CSEN 3135)
Module 1: Introduction to Python
Lecture 3: 25/10/2021

Dr. Debranjan Sarkar


More on Lists
• Concatenation produces a new list
• list1 = [2,4,6,8] list2 = list1 list1 = list1[0:2] + [10] + list1[3:]
• Now, list2 will remain [2,4,6,8] whereas list1 is [2,4,10,8]
• How to append a new item in the same list?
• list1 = [2,4,6,8] list1 = list1 + [10] # To append [10] to the list
• [10] is appended but to a new list so, list2 is not the same as list1
• list1.append(10) value 10 is appended in the same list (in-place)
• ‘append’ takes a single value
• For ‘append’, it is to be informed to Python that list1 is of type list. list1 = []
• To extend list1 by a list of values we use list1.extend(list2)
• list1 = [1,3,5], list2 = [7,9] list1.extend(list2) will produce [1,3,5,7,9]
• ‘extend’ is in-place equivalent of concatenation
• Removal of item from the list
• list1.remove(v) removes the first occurrence of v from the list
• Generates error if there is no v in the list
More on Lists
• List membership: (v in lst) returns True if the value v is found in the list lst
• To remove v from lst, with no error, if v not found in lst
if v in lst:
lst.remove(v)
• To remove all occurrences of v from lst
while v in lst:
lst.remove(v)
• lst.reverse() # To reverse the list lst in-place
• lst.sort() # To sort the list lst in ascending order
• lst.index(v) # To find out the leftmost position of v in lst
# Gives error if there is no v in lst
• lst.count(v) # To count the number of occurrences of v in lst
• And many more ……
Nested Lists
• An element of a list can be another list, which can have, in turn, another
list and so on
• This type of list is called a nested list. Example:
• nested_list = [[‘Tom’, ‘Harry’], 24, [True], [5, [10,15]]]
• nested_list[3] = [5, [10,15]]
• nested_list[3][1] = [10,15]
• nested_list[3][1][0] = 10
• nested_list[0][0][1] = ‘o’
• Example of updating a nested list in-place (mutability)
• nested_list[1] = 25 is allowed
• nested_list now becomes [[‘Tom’, ‘Harry’], 25, [True], [5, [10,15]]]
• nested_list[3][1][0] = 20
• nested_list now becomes [[‘Tom’, ‘Harry’], 25, [True], [5, [20,15]]]
Lists vs strings
• Both strings and lists can be indexed (or sliced)
• In case of string, single position as well as a slice return strings
• college = ‘Heritage’ => college[0] is ‘H’ college[0:2] is ‘He’
• For list, single position returns a value but a slice returns a list
• numbers = [4,8,12,16] => numbers[2] = 12 numbers[2:3] = [12]
• Strings are immutable whereas lists are mutable
• Lists can be nested
• List can be converted into a string by ‘/’.join([‘a’, ’b’, ’c’]) => ‘a/b/c’
• String can be converted into a list by list(‘xyz’) => [‘x’, ’y’, ’z’]
Equality (==) and Identity (is)
• list1 = [0,1,2,3,4] list2 = [0,1,2,3,4] list3 = list1
• list1 and list2 are two different lists with the same value
• If we update one list, the equality would not be preserved
• list1 and list3 are equal because both of them point to the same value
• Basically, they are the two names of the same list
• If we operate on any one of them, it still preserves the equality
• Two types of equality in case of mutable values such as lists:
• The objects are different but the value is same (equality)
• The objects are same and naturally the value is also same (identity)
• When the values are immutable (viz. numbers or strings) the distinction is largely
unimportant
• (list1 == list2) checks whether list1 and list2 have same value => True
• (list1 is list2) checks whether list1 and list2 refers to the same object => False
• (list3 is list1) checks whether list3 and list1 refers to the same object => True
• When (list3 is list1) => True, this implies that (list3 == list1) => True
Thank you

You might also like