Lists

A list is a data structure, that is mutable (or changeable). List items are placed inside square brackets[].
Important thing about a list is that the items in a list need not be of the same type.

Creating a List: The following example shows how to create list.

list1 = [10, 20, 30, 40, 50] print(list1) print(*list1)
[10, 20, 30, 40, 50] 10 20 30 40 50

Accessing List Values: To access list values, use index or indices of an element. Indexes starts from 0, index value of 1st element is 0, 2nd element is 1 and so on. Python also supports negative indexes which are count from right.

list1 = [10, 20, 30, 40, 50] print("Value at index 2 is:",list1[2]) print("Value at index -2 is:",list1[-2])
Value at index 2 is: 30 Value at index -2 is: 40

Updating List:You can modify the elements in the list. You can add new elements in a list with append() method.

list1 = [10, 20, 30, 40, 50] print("Value at index 2 is:",list1[2]) list1[2] = 25 print("New value at index 2 is:",list1[2])
Value at index 2 is: 30 New value at index 2 is: 25

Deleting Element from List: Using del statement you can remove element from list.

list1 = [10, 20, 30, 40, 50] print("Original list is:",list1) del list1[2] print("After deleting an element list is:",list1)
Original list is: [10, 20, 30, 40, 50] After deleting an element list is: [10, 20, 40, 50]

Basic List Operations:

  1. + Concatenate two lists and result is a new list.

    list1 = [10, 20, 30] list2 = [40, 50] print("list1 + list2 is ",list1+list2)
    list1 + list2 is [10, 20, 30, 40, 50]
  2. * Repetition of list for N times and result is a new list.

    list1 = [10, 20, 30] print("list1 * 2 is ",list1*2)
    list1 * 2 is [10, 20, 30, 10, 20, 30]
  3. in Membership operator which returns True when element present in list. False otherwise.

    list1 = [10, 20, 30] print("20 in list1 ",20 in list1) print("40 in list1 ",40 in list1)
    True False
  4. [start:stop:step] Gives access to a specified range of sequence’s elements.

    list1 = [10, 20, 30, 40, 50] print("list1[::] is",list1[::]) print("list1[2:] is",list1[2:]) print("list1[:3] is",list1[:3]) print("list1[::-1] is",list1[::-1])
    list1[::] is [10, 20, 30, 40, 50] list1[2:] is [30, 40, 50] list1[:3] is [10, 20, 30] list1[::-1] is [50, 40, 30, 20, 10]

List Methods:

  1. append()
  2. clear()
  3. copy()
  4. count()
  5. extend()
  6. index()
  7. insert()
  8. pop()
  9. remove()
  10. reverse()
  11. sort()

Practice Programs

  • Read list elements from the user.
    n = int(input("Enter number element to be insert: ")) list1 = [] print("Enter {} elements".format(n)) for i in range(n): list1.append(int(input())) print("List is",list1)
    Enter number element to be insert: 5 Enter 5 elements 1 3 2 5 4 List is [1, 3, 2, 5, 4]
  • Read list elements from the user using map() function.
    n = int(input("Enter number element to be insert: ")) print("Enter {} elements".format(n)) list1 = list(map(int, input().split()))[:n] print("List is",list1)
    Enter number element to be insert: 5 Enter 5 elements 1 3 2 5 4 List is [1, 3, 2, 5, 4]

Total Pageviews