Session – 12 (List)

  • List
  • List Operations
  • List Functions
  • List Methods

The list is a most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that items in a list need not be of the same type. Creating a list is as simple as putting different comma-separated values between square brackets

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]

Similar to string indices, list indices start at 0, and lists can be sliced, concatenated and so on.

1.List

  • Data structure
  • Data type
  • [ , , , , ] → Heterogeneous Elements
  • Position or index
  • First index is zero
  • Second index is one
  • Indexing
  • Slicing
  • Adding
  • Multiplying
  • Checking for membership
  • Length of a sequence
  • Finding largest elements
  • Finding smallest elements

2.List Operations

  • Accessing
  • Updating
  • Deleting
  • Indexing
  • Slicing
  • Matrixes
  • L = [ L0_1, L1_2, .. ,Lm_n]
  • L[x/-n]  L[x/:y/]
  • del L[x]
  • L_x+L_y – Concatenation
  • L*n – Repetition
  • x in L – Membership

3.List Functions

  • len(L) : no_ele_L
  • max(L) : Lar_L
  • min(L) : Sml_L
  • list(s) : s → L

4.List Methods

  • L.append(x) : L←[.,.,x]
  • L.count(x) : no_x_ele_L
  • L.extend(M) : L← [L, M]
  • L.index(x) : ele_x_posi_L
  • L.insert(n,x) : L←L[.,x@nth,.]
  • L.pop(/n) : L←pop_(end/nth)_ele_L
  • L.remove(x) : L←rmv_x_ele_L
  • L.reverse() : L ←rvs_L
  • L.sort() : L ←asc_L

Leave a comment