-->

Operations and Usage of List - Python



  • List is a data structure in python.
  • It is used to store a group of elements(not only similar type).
  • we can access any element with the index of element.
  • Python allows both positive and negative indexes.
  • Positive index starts from 0 and negative index starts from -1.

How to create new list ?

# empty list (case1)
l = list()
# empty list (case2)
l = []
# list with initial values
l = ["hello", "a", 123, 0.05, True]

How to add/append element to a list ?

l = []
l.append(123)
l.append("abc")
l.append(True)
print(l)
Output: [123, 'abc', True]

How delete/remove an element from a list ?

We can delete element from a list in two ways.
1.with element value
l = [1, 2, 3, 'a', 'b', 'c', 'abcd', 100, 200, 3]
l.remove(3)
print(l)
Output: [1, 2, 'a', 'b', 'c', 'abcd', 100, 200, 3]
# element that is not in list
l = [1, 2, 3, 'a', 'b', 'c', 'abcd', 100, 200, 3]
l.remove(1200)
Output: ValueError: list.remove(x): x not in list
'remove' method deletes first occurrence of the element. It will raise error if given value is not in list.
2. with element position(index)
l = [1, 2, 3, 'a', 'b', 'c', 'abcd', 100, 200, 3]
deleted_element = l.pop(6)
# this will deletes the element at position 6(note: position/index starts from 0)
print(deleted_element)
Output: 'abcd'
print(l)
# Output: [1, 2, 3, 'a', 'b', 'c', 100, 200, 3]
deleted_element = l.pop(-1)
print(deleted_element)
# Output: 3
print(l)
# Output: [1, 2, 3, 'a', 'b', 'c', 100, 200]
# raises error if the given index is out of the range
deleted_element = l.pop(1200)
# Output: IndexError: pop index out of range

How to find index of an element in a list ?

l = [1, 2, 3, 'a', 'b', 'c', 'abcd', 100, 200, 3]
index = l.index(3)
print(index)
# Output: 2
# Note: index method returns the position of first occurence of value. raises error if given value not in list.
index = l.index("not found")
# Output: ValueError: 'not found' is not in list

How to find number of occurrences of an element in a list ?


l = [1, 2, 3, 'a', 'b', 'c', 'abcd', 100, 200, 3, 1, 2, 3, 5, 3]
c = l.count(3)
print(c)
# Output: 4

How to add a list of elements to another list using "extend" method?


l1 = [1,2,3]
l2 = [10, 20, 30]
l1.extend(l2)
print(l1)
# Output: [1, 2, 3, 10, 20, 30]
print(l2)
# Output: [10, 20, 30]
Note: extend modifies state of the list

How reverse order of elements in a list ?


l = [10, 20, 30]
l.reverse()
print (l)
# Output: [30, 20, 10]

How to sort a list of elements in python ?


l = [1, 2, 3, 'a', 'b', 'c', 'abcd', 100, 200, 3, 1, 2, 3, 5, 3]
l.sort()
print(l)
# Output: [1, 1, 2, 2, 3, 3, 3, 3, 5, 100, 200, 'a', 'abcd', 'b', 'c']
# For descending order
l = [1, 2, 3, 'a', 'b', 'c', 'abcd', 100, 200, 3, 1, 2, 3, 5, 3]
l.sort(reverse=True)
print(l)
# Output: ['c', 'b', 'abcd', 'a', 200, 100, 5, 3, 3, 3, 3, 2, 2, 1, 1]
Note: It modifies state of the list.

How to sort the list without modifying original list ?


l = [1, 2, 3, 'a', 'b', 'c', 'abcd', 100, 200, 3, 1, 2, 3, 5, 3]
new_list = sorted(l)
print(new_list)
# Output: [1, 1, 2, 2, 3, 3, 3, 3, 5, 100, 200, 'a', 'abcd', 'b', 'c']
print(l)
# Output: [1, 2, 3, 'a', 'b', 'c', 'abcd', 100, 200, 3, 1, 2, 3, 5, 3]

How to insert an element at a specific position list?


l = [1, 2, 3, 'a', 'b']
l.insert(1, "new")
print(l)
# Output: [1, 'new', 2, 3, 'a', 'b']

How to update a value at a specific position in list?


l = [1, 2, 3, 'a', 'b']
l[1] = "updated"
print(l)
# Output: [1, 'updated', 3, 'a', 'b']

Buy a product to Support me