Page 199 - Information_Practice_Fliipbook_Class11
P. 199

08
              09 noDuplicateList = []
              10 for element in myList:
              11     if element not in noDuplicateList:
              12         #append  it to noDuplicateList
              13         noDuplicateList.append(element)
              14
              15 print('List with no duplicates:' , noDuplicateList)
            Sample Output:

             >>> Enter the list:
                 [4, 6, 2, 5, 6, 3, 6, 4, 9, 2, 4, 6, 7, 4, 6, 7]
                 Original List: [4, 6, 2, 5, 6, 3, 6, 4, 9, 2, 4, 6, 7, 4, 6, 7]
                 List with no duplicates: [4, 6, 2, 5, 3, 9, 7]
            Program 7.5 Given a list (say, list1) of numbers, its cumulative list is another list (say, list2) whose ith element is the
            sum of all elements till the ith element of list1. If a list is empty then its cumulative list will also be an empty list.
            Write a program that accepts a list of integers and displays its cumulative list.

              01 '''
              02 Objective: To find the cumulative sum of elements of an integer list
              03 Input: a list of numbers
              04 output: cumulative sum of elements of the input list
              05 '''
              06
              07 myList = eval(input('Enter a list of numbers: '))
              08
              09 if len(myList) == 0 :
              10     cumLst = []
              11 else:
              12     cumLst = [myList[0]] # myList[0] is sum of 1-element list
              13     for i in range(1,len(myList)):
              14         nextElement = cumLst[i-1] + myList[i]
              15         cumLst.append(nextElement)
              16 print('List of cumulative sums: ' ,cumLst)
            Sample Output:

             >>> Enter a list of numbers: [10,20,30,40,50,60]
                 List of cumulative sums:  [10, 30, 60, 100, 150, 210]



                 Let's Summarise



              Ø   A list comprises a sequence of objects enclosed in square brackets [].
              Ø   A pair of square brackets [] denotes an empty list.
              Ø   The len() function returns the number of objects in a list.

              Ø   (+) operator is used to concatenates a pair of list.
              Ø   (*) operator concatenates a list for the specified number of times and yields a new list.
              Ø   Traversing a list means accessing each element of a list. This can be done by using looping statement, either
                  for or while.
              Ø   List Methods: Methods are defined for list objects. These methods are as follows:
              Ø   append(elem): inserts the object elem, passed as an argument, at the end of the list.

              Ø   insert (index, elem): Inserts the object elem, passed as an argument, at the specified index and
                  shifts the existing objects to the right.



                                                                                                   Python Lists  185
   194   195   196   197   198   199   200   201   202   203   204