List Functions

M3-R5.1 · Chapter 5: Sequence Data Types · 6 min read

Built-in List Functions

Detailed Examples

1.  append( )

The append() method adds a single element to the end of the list. It takes exactly one argument — if you pass more than one, Python raises a TypeError.

Syntax:   list_name.append(element)

Example:

>>> company = ["IBM", "HCL", "Wipro"]

>>> company.append("Google")

>>> company

['IBM', 'HCL', 'Wipro', 'Google']

Common Error:

>>> company.append("infosys", "microsoft")

TypeError: append() takes exactly one argument (2 given)

2.  extend( )

The extend() method adds all elements of another list to the end of the current list. Unlike append(), it merges the two lists together. It takes a list as its single argument.

Syntax:   list_name.extend(another_list)

Example:

>>> company = ["IBM", "HCL", "Wipro"]

>>> desktop = ["dell", "HP"]

>>> company.extend(desktop)

>>> company

['IBM', 'HCL', 'Wipro', 'dell', 'HP']

Common Error:

>>> company.extend("dell", "HP")

TypeError: extend() takes exactly one argument (2 given)

3.  len( )

The len() function returns the total number of elements in the list. Note: a nested list counts as a single element, not the items it contains.

Syntax:   len(list_name)

Example:

>>> company = ["IBM", "HCL", "Wipro"]

>>> len(company)

3

>>> L = [3, 6, [5, 4]]

>>> len(L)

3    # The nested list [5, 4] counts as 1 element

4.  index( )

The index() method returns the index position of the first occurrence of a specified element. Python is case-sensitive, so 'Wipro' and 'WIPRO' are treated as different values.

Syntax:   list_name.index(element)

Example:

>>> company = ["IBM", "HCL", "Wipro", "HCL", "Wipro"]

>>> company.index("Wipro")

2    # Returns index of FIRST occurrence

Common Errors:

>>> company.index("WIPRO")   # Wrong case

ValueError: 'WIPRO' is not in list

>>> company.index(2)   # Don't pass index number; pass the element value

ValueError: 2 is not in list

5.  insert( )

The insert() method adds an element at a specified position in the list. It takes two arguments: the index and the element. If the index is beyond the list length, the element is added at the end. A negative out-of-range index inserts at the beginning.

Syntax:   list_name.insert(index, element)

Example:

>>> company = ["IBM", "HCL", "Wipro"]

>>> company.insert(2, "Apple")

>>> company

['IBM', 'HCL', 'Apple', 'Wipro']

>>> company.insert(16, "Microsoft")   # Index beyond length → adds at end

['IBM', 'HCL', 'Apple', 'Wipro', 'Microsoft']

>>> company.insert(-16, "TCS")   # Negative out-of-range → adds at start

['TCS', 'IBM', 'HCL', 'Apple', 'Wipro', 'Microsoft']

6.  count( )

The count() method returns the number of times a specified element appears in the list. It returns 0 if the element is not found — it does not raise an error.

Syntax:   list_name.count(element)

Example:

>>> company = ["IBM", "HCL", "Wipro", "HCL", "Wipro"]

>>> company.count("HCL")

2

>>> company.count("TCS")

0    # Returns 0 if not found — no error raised

7.  remove( )

The remove() method removes the first occurrence of the specified element. If the element does not exist in the list, it raises a ValueError.

Syntax:   list_name.remove(element)

Example:

>>> company = ["IBM", "HCL", "Wipro", "HCL", "Wipro"]

>>> company.remove("Wipro")   # Only first "Wipro" is removed

>>> company

['IBM', 'HCL', 'HCL', 'Wipro']

Common Error:

>>> company.remove("Yahoo")

ValueError: list.remove(x): x not in list

8.  clear( )

The clear() method removes all elements from the list, leaving it empty. Unlike del list_name, the list object itself still exists after clear() — it just becomes [].

Syntax:   list_name.clear()

Example:

>>> company = ["IBM", "HCL", "Wipro"]

>>> company.clear()

>>> company

[]    # List still exists, just empty

9.  pop( )

The pop() method removes and returns the element at the given index. The index argument is optional — if no index is provided, pop() removes and returns the last element. Raises IndexError on empty lists.

Syntax:   list_name.pop(index)   # index is optional

Example:

>>> company = ["IBM", "HCL", "Wipro"]

>>> company.pop(1)

'HCL'    # Removed and returned

>>> company

['IBM', 'Wipro']

>>> company.pop()   # No index → removes last element

'Wipro'

Common Error:

>>> L = []

>>> L.pop()

IndexError: pop from empty list

10.  copy( )

The copy() method returns an independent shallow copy of the list. Changes made to the copy do not affect the original list, and vice versa.

Syntax:   new_list = list_name.copy()

Example:

>>> company = ["IBM", "HCL", "Wipro"]

>>> L = company.copy()

>>> L

['IBM', 'HCL', 'Wipro']    # Independent copy of the original

11.  reverse( )

The reverse() method reverses the order of elements in-place. It does not take any arguments and does not return a new list — it returns None and modifies the original list directly.

Syntax:   list_name.reverse()   # No argument, modifies original

Example:

>>> company = ["IBM", "HCL", "Wipro"]

>>> company.reverse()

>>> company

['Wipro', 'HCL', 'IBM']

12.  sort( )

The sort() method sorts the list in-place in ascending order by default. To sort in descending order, pass reverse=True as an argument. Like reverse(), it modifies the original list and returns None.

Syntax (Ascending):   list_name.sort()

Syntax (Descending):  list_name.sort(reverse=True)

Example:

>>> company = ["IBM", "HCL", "Wipro"]

>>> company.sort()

>>> company

['HCL', 'IBM', 'Wipro']    # Ascending order

>>> company = ["IBM", "HCL", "Wipro"]

>>> company.sort(reverse=True)

>>> company

['Wipro', 'IBM', 'HCL']    # Descending order