Python Data Types LIST
List • A list is a collection of items or elements • The values in a list can be of any type and are called elements or items. • The elements or items in a list can be accessed by their positions, i.e., indices. • The most convenient way is using square brackets ([]).
List >>> list1=[1,2,[77,89]] >>> print(list1) [1, 2, [77, 89]]
Copying the list 1.Using Assignment Operator >>>list_org1=[1,2,3,5] >>> list_copy1=list_org1 >>> print(list_org1) [1, 2, 3, 5] >>> print(list_copy1) [1, 2, 3, 5] >>> list_org1.append(10) >>> print(list_org1) [1, 2, 3, 5, 10] >>> print(list_copy1) [1, 2, 3, 5,10] [1, 2, 3, 5] list_org1 list_copy1
Copying the list using [:] Making a copy of the list >>>list_org1=[1,2,3,5] >>> list_copy1=list_org1[:] >>> print(list_org1) [1, 2, 3, 5] >>> print(list_copy1) [1, 2, 3, 5] >>> list_org1.append(10) >>> print(list_org1) [1, 2, 3, 5, 10] >>> print(list_copy1) [1, 2, 3, 5] [1, 2, 3, 5,10] [1, 2, 3, 5] list_org1 list_copy1
Copying the list using copy() function Making a copy of the list >>> list1=[12,3,9,8] >>> from copy import copy >>> list2=copy(list1) >>> print(list1) [12, 3, 9, 8] >>> print(list2) [12, 3, 9, 8] >>> list1.append(100) >>> print(list1) [12, 3, 9, 8, 100] >>> print(list2) [12, 3, 9, 8]
List characteristics 1. List are Mutable >>> list1=[10,20,30,40] >>> print(list1[3]) 40 >>> list1[3]=50 >>> print(list1) [10, 20, 30, 50] 2. A empty list can be create using enclosing brackets with no elements inside >>>list2=[] >>> print(len(list2)) 0 3. Every element or item inside a list always has an index number through which it is accessed
Traversing a list • Traversing a list means accessing all the elements or items of the list. • Traversing can be done by using any conditional statement of Python, >>> list1=['a','b','c','d'] for x in list1: print(x) a b c d
Traversing a list >>>list1=[10,20,30,40] >>> for i in range(len(list1)): list1[i]=list1[i]+4 >>> print(list1) [14, 24, 34, 44]
Deleting element from list 1. Pop • The pop operator deletes the element on the provided index and stores that element in a variable for further use. >>> list1=[10,20,30,40] >>> a=list1.pop(2) >>> print(list1) [10, 20, 40] >>> print("deleted element",a) deleted element 30
Deleting element from list 2. Del • The del operator deletes the value on the provided index, but it does not store the value for further use. list1=[11,22,33,44] >>> del list1[1] >>> print(list1) [11, 33, 44] 3. Remove • We use the remove operator if we know the item that we want to remove or delete from the list (but not the index). list1=[100,200,300,400] >>> list1.remove(400) >>> print(list1) [100, 200, 300]
Deleting more than one element from list • In order to delete more than one value from a list, del operator with slicing is used. >>> list1=[1,4,3,9,20,32] >>> del list1 [1:4] >>> print(list1) [1, 20, 32]
Built-in list operators 1.operator(+) >>> list1=[10,20,30] >>> list2=[40,50] >>> list3=list1+list2 >>> print(list3) [10, 20, 30, 40, 50] 2.Operator(*) >>> print(list1*3) [10, 20, 30, 10, 20, 30, 10, 20, 30] 3.operator(in) >>> print(10 in list1) True >>> print(50 in list1) False
Built-in list methods
Built-in list methods >>> l1=[13,32,69] >>> print(min(l1)) 13 >>> print(max(l1)) 69 >>> print(len(l1)) 3
Built-in list methods >>> l1=[12,43,90] >>> print(l1) [12, 43, 90] >>> l2=[55,66,77] >>> l1.append(l2) >>> print(l1) [12, 43, 90, [55, 66, 77]] >>> l1=[11,79,34] >>> l2=[44,55,66] >>> l1.extend(l2) >>> print(l1) [11, 79, 34, 44, 55, 66] >>> vowels=['a','e','o','u'] >>> vowels.insert(2,'i') >>> print(vowels) ['a', 'e', 'i', 'o', 'u']
Built-in list methods >>> l3=['a','e','i','o','u'] >>> print(l3.index('i')) 2 >>> print(l3.index('b')) Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> print(l3.index('b')) ValueError: 'b' is not in list >>> l4=['a','e','i','o','u','a','e'] >>> print(l4.index('a')) 0
Built-in list methods >>> l4=['a','e','i','o','u','a','e'] >>> print(l4.count('a')) 2 >>> print(l4.count('i')) 1 >>> print(l4.count('x')) 0 >>>vowels=['a', 'e', 'i', 'o', 'u'] >>> vowels.clear() >>> print(vowels) []
Built-in list methods >>> list1=[22,10,34,45,30] >>> list1.reverse() >>> print(list1) [30, 45, 34, 10, 22] >>> list1=[12,31,20,10,42] >>> list1.sort() >>> print(list1) [10, 12, 20, 31, 42] >> list1.sort(reverse=True) >>> print(list1) [42, 31, 20, 12, 10]
Built-in list methods >>> l1=[1,2,3] >>> l2=[1,2] >>> cmp(l1,l2) 1 >>> cmp(l2,l1) -1 >>> l3=[5,6] >>> cmp(l1,l3) -1 >>> cmp(l3,l1) 1 >>> l4=[1,2] >>> cmp(l2,l4) 0

Python list concept

  • 1.
  • 2.
    List • A listis a collection of items or elements • The values in a list can be of any type and are called elements or items. • The elements or items in a list can be accessed by their positions, i.e., indices. • The most convenient way is using square brackets ([]).
  • 4.
  • 5.
    Copying the list 1.UsingAssignment Operator >>>list_org1=[1,2,3,5] >>> list_copy1=list_org1 >>> print(list_org1) [1, 2, 3, 5] >>> print(list_copy1) [1, 2, 3, 5] >>> list_org1.append(10) >>> print(list_org1) [1, 2, 3, 5, 10] >>> print(list_copy1) [1, 2, 3, 5,10] [1, 2, 3, 5] list_org1 list_copy1
  • 6.
    Copying the listusing [:] Making a copy of the list >>>list_org1=[1,2,3,5] >>> list_copy1=list_org1[:] >>> print(list_org1) [1, 2, 3, 5] >>> print(list_copy1) [1, 2, 3, 5] >>> list_org1.append(10) >>> print(list_org1) [1, 2, 3, 5, 10] >>> print(list_copy1) [1, 2, 3, 5] [1, 2, 3, 5,10] [1, 2, 3, 5] list_org1 list_copy1
  • 7.
    Copying the listusing copy() function Making a copy of the list >>> list1=[12,3,9,8] >>> from copy import copy >>> list2=copy(list1) >>> print(list1) [12, 3, 9, 8] >>> print(list2) [12, 3, 9, 8] >>> list1.append(100) >>> print(list1) [12, 3, 9, 8, 100] >>> print(list2) [12, 3, 9, 8]
  • 8.
    List characteristics 1. Listare Mutable >>> list1=[10,20,30,40] >>> print(list1[3]) 40 >>> list1[3]=50 >>> print(list1) [10, 20, 30, 50] 2. A empty list can be create using enclosing brackets with no elements inside >>>list2=[] >>> print(len(list2)) 0 3. Every element or item inside a list always has an index number through which it is accessed
  • 9.
    Traversing a list •Traversing a list means accessing all the elements or items of the list. • Traversing can be done by using any conditional statement of Python, >>> list1=['a','b','c','d'] for x in list1: print(x) a b c d
  • 10.
    Traversing a list >>>list1=[10,20,30,40] >>>for i in range(len(list1)): list1[i]=list1[i]+4 >>> print(list1) [14, 24, 34, 44]
  • 11.
    Deleting element fromlist 1. Pop • The pop operator deletes the element on the provided index and stores that element in a variable for further use. >>> list1=[10,20,30,40] >>> a=list1.pop(2) >>> print(list1) [10, 20, 40] >>> print("deleted element",a) deleted element 30
  • 12.
    Deleting element fromlist 2. Del • The del operator deletes the value on the provided index, but it does not store the value for further use. list1=[11,22,33,44] >>> del list1[1] >>> print(list1) [11, 33, 44] 3. Remove • We use the remove operator if we know the item that we want to remove or delete from the list (but not the index). list1=[100,200,300,400] >>> list1.remove(400) >>> print(list1) [100, 200, 300]
  • 13.
    Deleting more thanone element from list • In order to delete more than one value from a list, del operator with slicing is used. >>> list1=[1,4,3,9,20,32] >>> del list1 [1:4] >>> print(list1) [1, 20, 32]
  • 14.
    Built-in list operators 1.operator(+) >>>list1=[10,20,30] >>> list2=[40,50] >>> list3=list1+list2 >>> print(list3) [10, 20, 30, 40, 50] 2.Operator(*) >>> print(list1*3) [10, 20, 30, 10, 20, 30, 10, 20, 30] 3.operator(in) >>> print(10 in list1) True >>> print(50 in list1) False
  • 15.
  • 16.
    Built-in list methods >>>l1=[13,32,69] >>> print(min(l1)) 13 >>> print(max(l1)) 69 >>> print(len(l1)) 3
  • 17.
    Built-in list methods >>>l1=[12,43,90] >>> print(l1) [12, 43, 90] >>> l2=[55,66,77] >>> l1.append(l2) >>> print(l1) [12, 43, 90, [55, 66, 77]] >>> l1=[11,79,34] >>> l2=[44,55,66] >>> l1.extend(l2) >>> print(l1) [11, 79, 34, 44, 55, 66] >>> vowels=['a','e','o','u'] >>> vowels.insert(2,'i') >>> print(vowels) ['a', 'e', 'i', 'o', 'u']
  • 18.
    Built-in list methods >>>l3=['a','e','i','o','u'] >>> print(l3.index('i')) 2 >>> print(l3.index('b')) Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> print(l3.index('b')) ValueError: 'b' is not in list >>> l4=['a','e','i','o','u','a','e'] >>> print(l4.index('a')) 0
  • 19.
    Built-in list methods >>>l4=['a','e','i','o','u','a','e'] >>> print(l4.count('a')) 2 >>> print(l4.count('i')) 1 >>> print(l4.count('x')) 0 >>>vowels=['a', 'e', 'i', 'o', 'u'] >>> vowels.clear() >>> print(vowels) []
  • 20.
    Built-in list methods >>>list1=[22,10,34,45,30] >>> list1.reverse() >>> print(list1) [30, 45, 34, 10, 22] >>> list1=[12,31,20,10,42] >>> list1.sort() >>> print(list1) [10, 12, 20, 31, 42] >> list1.sort(reverse=True) >>> print(list1) [42, 31, 20, 12, 10]
  • 21.
    Built-in list methods >>>l1=[1,2,3] >>> l2=[1,2] >>> cmp(l1,l2) 1 >>> cmp(l2,l1) -1 >>> l3=[5,6] >>> cmp(l1,l3) -1 >>> cmp(l3,l1) 1 >>> l4=[1,2] >>> cmp(l2,l4) 0