Lists in Python PYTHON by Ravinder Nath Rajotiya - April 19, 2019March 12, 20220 Share on Facebook Share Send email Mail Print Print Lists List is the most common composite data type in Python. It is is an ordered set of elements. Elements in a list can be of similar or different types. Declaration: A list in python is enclosed inside [ ] bracket. Elements in the list are separated by comma. Example of a list is: list1 = [8,6,7,4,5,3] When a list is created in Python, the interpreter creates a data structure in memory that is similar to an array (but not an array) to store it. The numbering in the list starts with 0, then 1, and so on, if accessed from the end indexing starts from -1, -2 and so on. myList = [3,2,6,4,9,’a’, ‘b’,’c’,‘ram’,’s’] myList 3 2 6 4 ‘a’ ‘b’ ‘c’ ‘ram’ ‘s’ Index from begining 0 1 2 3 4 5 6 7 8 Indexing from end -9 -8 -7 -6 -5 -4 -3 -2 -1 Real-Time Usage of the List The list data type is used as arrays, data validation and validating user registration data with regular expression validation like name, email, id, contact details. In Django we use list in installed apps INSTALLED_APP=[ ‘blogs’, ‘myApp’, ….], directory DIR=[‘template’, ‘myApp/template’,] etc Operations on the list: A list is a mutable item whic means it can be modified and we can perform following operations on a list: Access : we can access any element the following ways: myList – access all element of a list myList[2] – access 2nd (2-1) element (element at index is 1st element) myList[0: ] – access all element starting from index 0 myList[2: 5] – access all element starting from index 0 upto 4th (5-1) element myList[ : 4] – access all elements from 1st to 4th (index 0 to index (N-1)) Update (append and insert): Example-1 >>> squares = [1, 4, 10, 16, 25] >>> squares [1, 4, 9, 16, 25] >>>squares[2]= 9 # this changes the value of 2nd item >>>square +[36, 49, 64, 81,100] >>>Squares 1,4,9,16,25,36,49,64,81,100 Example-2 >>> x1 = [2, ‘a’, ‘b’, ‘c’, ‘d’] then >>> x1 [2, ‘a’, ‘b’, ‘c’, ‘d’] >>> x1[2:4]=[‘B’,’C’] will append element 2nd and (4-1)rd element >>> x1 [2, ‘a’, ‘B’, ‘C’, ‘d’ ] Delete elements (del) : del is used to remove an item from a list given its index instead of its value. It can also be used to remove slices from a list or clear the entire list. It differs from the pop() method which returns a value, but del() does not return the value. >>>myList = [3,4,5,6,7] >>>del myList[3] will delete the 3rd element >>>myList [3,4,5,7] >>>myList[ : ] = [ ] # will clear the list, list now is empty >>>myList [ ] displays an empty list Operator on list script (+/*): Arithmetic Operations can be performed on elements of a list >>> list3=[2+2,4/2,’a’+’b’+’c’, 8-5, 3*3] >>>list3 [4, 2.0, ‘abc’, 3, 9] Concatenation of Lists >>> list1=[1,2,3,4,5] >>> list2=[1,2,3] >>> list3=list1+list2 >>> list3 [1, 2, 3, 4, 5, 1, 2, 3] Creating nested List (List within a list) In python we can have the nested lists, it can be obtained as shown below: >>> list1 = [1,2,3,4,5] >>> list2 = [1,2,3] >>> list3 = [list1,list2] >>> list3 [ [1, 2, 3, 4, 5], [1, 2, 3] ] list3 now has two elements one at index-0 and one at index-1. List 3 now has two slices one at index-0 and contains 5 elements and slice-2 contains 3 elements. The elements of the the slice can be obtained as: >>>list3[0] it will display [1, 2, 3, 4, 5] >>>list3[0][2] will display 2nd element of slice-1 3 >>>list3[1] [1, 2, 3] List3[1][1] will display element at index-1 in slice-2 2 >>>list1=[2,3,4,5] >>>list2=[3,4,5] >>>list3=[ list1, ‘a’, list2, 7 ] [ [2, 3, 4, 5], ‘a’, [3,4,5], 7 ] Here list3 has 4 slices, slice-1 has four elements, slice-2 has one element, slice-3 has three elements and slice-4 has one element >>>list3[2][1] 4 Common functions on List len(list) : will display number of list elements >>>x1 = [ 2, 3, 4, 5 ] >>>len(x1) 4 max(list) : It will returns the maximum value of list elements >>>x1 = [ 2, 3, 4, 5 ] >>>max(x1) 5 min(list) : It will returns the minimum value of list elements >>>x1 = [ 2, 3, 4, 5 ] >>>min(x1) 2 list(seq) : It will converts tuple into list Methods of Python Listing method description example >>>lis1=[1,4,9,16,25,36] list.append(obj) appends a new object at the end of a list >>>list1.append(49) >>>list [1, 4, 9, 16, 25, 36, 49] list.count(obj) counts the repetitions of a given element in a list >>>list2=[2,4,4,4,5,6,7] >>>list2.count(4) 3 list.extend(seq) adds multiple values of another sequence at the end of a list (using a new list to extend the original list) >>>list1.extend(list2) >>>list1 [1,4,9,16,25,36, 2,4,4,4,5,6,] list.index(obj) finds the indexing location with the first match with a value in the list >>>list2.index(4) 1 list.insert(index, obj) inserts an object in the list >>>list2=[2,4,4,4,5,6,7] >>>list2.insert(1,9) >>>list2 [2,9,4,4,4,5,6,7] list.pop(obj=list[-1]) removes an element (last one by default) from the list and returns value of the element >>>list2.pop(-2) >>>list2 [2,4,4,4,5,7] list.remove(obj) removes the first match with a given value from the list >>>list2=[2,3,9,4,5,6] >>>list2.remove(9) >>>list2 [2,3,4,5,6] list.reverse() reverse elements in a list >>>list2=[2,3,4,5,6] >>>list2.reverse() [6,5,4,3,2] list.sort([func]) sorting the original list >>>list2=[6,2,4,3,1,5,7] >>>list2.sort() >>>list2 [1,2,3,4,5,6,7] Share on Facebook Share Send email Mail Print Print