List Methods in python

List Methods in python

Array in C is a collection of similar typed elements. While the list is a collection of similar or heterogeneous typed elements. The size of the array in the C programming language is fixed. Once the array is defined with a particular size, then the size of the array cannot be changed. But in python, the size of the list can be increased or decreased as the need arises. The List Methods in python are used to update the size of the list.

Video Tutorial

Following are the usually used methods on lists in the Python programming language.

append
extend
sort
remove
pop
del

Append Method

The append() method is used to add a new element to the end of a list. The following program demonstrates the usage of the append() method on the list in python.

l1  = [10, 20, 30]
print ("Before append", l1)
l1.append(40)
print ("After append",l1)

Output

Before append [10, 20, 30]
After append [10, 20, 30, 40]

Another Example

t1 = ['a', 'b', 'c']
t2 = ['x', 'y']

print ("Before Append", t1)

t1.append(t2)

print ("After Append", t1)

In tis case argument is list. But append considers it as an element and appends it as one element. The solution to this is extend method (explained later)

Output

Before Append ['a', 'b', 'c']
After Append ['a', 'b', 'c', ['x', 'y']]

Extend Method

The extend() method takes a list as an argument and is used to append the element of one list to another. The following program demonstrates the usage of the extend() method on the list in python.

t1 = ['a', 'b', 'c']
t2 = ['x', 'y']

print ("Before Extend", t1)

t1.extend(t2)

print ("After Extend", t1)

Before Extend ['a', 'b', 'c']
After Extend ['a', 'b', 'c', 'x', 'y']

Sort Method

The sort() method is used to arrange the elements of the list from low to high (ascending order). The following program demonstrates the usage of the sort() method on the list in python.

t = ['d', 'c', 'e', 'b', 'a']

print ("Before Sort", t)

t.sort()

print ("After Sort", t)

Output

Before Sort ['d', 'c', 'e', 'b', 'a']
After Sort ['a', 'b', 'c', 'd', 'e']

The elements of the list can be sorted in descending order that is from high to low by setting the reverse attribute to True. The following program demonstrates the usage of the sort() method to sort elements of the list in descending order in python.

t = ['d', 'c', 'e', 'b', 'a']

print ("Before Sort", t)

t.sort(reverse=True)

print ("After Sort", t)

Output

Before Sort ['d', 'c', 'e', 'b', 'a']
After Sort ['e', 'd', 'c', 'b', 'a']

pop method

The pop() modifies the list and returns the element that was removed. If you don’t provide an index, it deletes and returns the last element. By default, pop removes the last element of the list. The following program demonstrates the usage of the pop() method on the list in python.

t = ['a', 'b', 'c', 'd', 'e']

print ("Before Pop", t)

x = t.pop()

print ("After Pop", t)

print ("Popped Element:", x)

Output

Before Pop ['a', 'b', 'c', 'd', 'e']
After Pop ['a', 'b', 'c', 'd']
Popped Element: e

Another example to demonstrate pop method

t = ['a', 'b', 'c', 'd', 'e']

print ("Before Pop", t)

x = t.pop(2)

print ("After Pop", t)

print ("Popped Element:", x)

Output

Before Pop ['a', 'b', 'c', 'd', 'e']
After Pop ['a', 'b', 'c', 'd']
Popped Element: c

del Method

The del() method is used to remove an element, but it will not return the removed element. The following program demonstrates the usage of the del() method on the list in python.

t = ['a', 'b', 'c', 'd', 'e']

print ("Before Delete", t)

del t[1:4]

print ("After Delete", t)

Output

Before Delete ['a', 'b', 'c', 'd', 'e']
After Delete ['a', 'e']

Another example to demonstrate the del() method

t = ['a', 'b', 'c', 'd', 'e']

print ("Before Delete", t)

del t[:2]

print ("After Delete", t)

Output

Before Delete ['a', 'b', 'c', 'd', 'e']
After Delete ['c', 'd', 'e']

remove method

If you know the element you want to remove from the list (but not the index), you can use the remove() method. The following program demonstrates the usage of the remove() method on the list in python.

t = ['a', 'b', 'c', 'd', 'e']

print ("Before remove", t)

t.remove('b')

print ("After remove", t)

Output

Before Delete ['a', 'b', 'c', 'd', 'e']
After Delete ['a', 'c', 'd', 'e']

Summary:

This tutorial discusses the usage of different List Methods in python. If you like the tutorial do share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published.