#12 - List and Tuple Operations

A few of the basic list operations used in Python programming are

  1. extend()
  2. insert()
  3. append()
  4. remove()
  5. pop()
  6. min()
  7. max()
  8. sort()
  9. sorted()
  10. index()
  11. clear()
  12. list()
  13. tuple() etc.


1. append()


The append() method is used to add elements at the end of the list. This method can only add a single element at a time.

EXAMPLE:

  1. list1 = [1,2,3,4,5]
  2. list1.append(6) # adding 6 at the end
  3. list1.append(7) # adding 7 at the end
  4. print(list1)

OUTPUT:

[1,2,3,4,5,6,7]


2. extend()

The extend() method is used to add more than one element at the end of the list. Although it can add more than one element, unlike append(), it adds them at the end of the list like append()

EXAMPLE:

  1. list1 = [1,2,3,4,5]
  2. list1.extend([8,9,10]) # adding 8,9,10 at the end, append use to add single element , extend is use to add multilple elements at the end
  3. print(list1)


OUTPUT:

[1,2,3,4,5,8,9,10]


3. insert()



The insert() method can add an element at a given position in the list. Thus, unlike append(), it can add elements at any position, but like append(), it can add only one element at a time. This method takes two arguments. The first argument specifies the position, and the second argument specifies the element to be inserted.

EXAMPLE:

  1. list1 = [1,2,3,4,5]
  2. list1.insert(0, -100) # adding -100 at the 0th index
  3. print(list1)


OUTPUT:

[-100, 1, 2, 3,4,5]


4. remove()


The remove() method is used to remove an element from the list. In the case of multiple occurrences of the same element, only the first occurrence is removed.

EXAMPLE:

list1 = [1,2,3,4,5]
list1.remove(5)
print(list1)


OUTPUT:

[1,2,3,4]


5. pop()

The method pop() can remove an element from any position in the list. The parameter supplied to this method is the index of the element to be removed.

EXAMPLE:

  1. list1 = [1,2,3,4,5]
  2. list1.pop() # if the index in not specified then the last element will be deleted
  3. list1.pop(0) # this deletes the element at 0th index


OUTPUT:

[2,3,4]


6. len()



The len() method returns the length of the list, i.e. the number of elements in the list.

EXAMPLE:

list1 = [1,2,3,4,5]
print(len(list1))


OUTPUT:

5


7. min() & max()



The min() method returns the minimum value in the list. The max() method returns the maximum value in the list. Both the methods accept only homogeneous lists, i.e. list having elements of similar type.

EXAMPLE:

  1. list1 = [1,2,3,4,5]
  2. print(min(list1)) # minimum of the list
  3. print(max(list1)) # maximum of the list


OUTPUT:

1
5


8. sort()



The sort method sorts the list in ascending order. This operation can only be performed on homogeneous lists, i.e. lists having elements of similar type.
SORT METHOD MAKE CHANGES TO THE ORIGINAL LIST .

EXAMPLE:

list1 = [5,3,6,23,2]
list1.sort()
print(list1)


OUTPUT:

[2,3,5,6,23]


9.sorted()



The sort method sorts the list in ascending order. This operation can only be performed on homogeneous lists, i.e. lists having elements of similar type.
UNLIKE SORT , SORTED METHOD RETURNS A NEW LIST AND THE ORIGINAL LIST IS NOT CHANGED.

EXAMPLE:

  1. list1 = [5,3,6,23,2]
  2. l1 = sorted(list1)
  3. print(list1) #original list
  4. print(l1) #new sorted list


OUTPUT:

[5,3,6,23,2]
[2,3,5,6,23]


10. clear()



clear method is used to delete all the elements in the list , but the list in not deleted .

EXAMPLE:

list1 = [2,34,5,6,3]
list1.clear()
print(list1)


OUTPUT:

[]


11. list()

This method is used to convert the give arguments into a list . We can convert a tuple in to a list, a set into a list .

EXAMPLE:

tu = (1,2,3,4)
st = {1,2,3,4}
l1 = list(tu)
l2 = list(st)
print(l1)
print(l2)


OUTPUT:

[1,2,3,4]
[1,2,3,4]


12. tuple()



This method is used to convert the give arguments into a tuple. We can convert a list in to a tuple, a set into a tuple .

EXAMPLE:

l1 = [1,2,3,4]
l2 = {1,2,3,4}
t1 = tuple(l1)
t2 = tuple(l2)
print(t1)
print(t2)


OUTPUT:

(1,2,3,4)
(1,2,3,4)