List and List Methods

Introduction to Lists

  • Ordered, Mutable collection of data separated by comma , and enclosed in []
  • Items of a list can be of any valid Python data types
  • Examples:
    1. int_list = [1, 2, 3] str_list = ["apple", "banana", "orange"] mixed_list = [1, "hello", 3.14, True] nested_list = [1, [2, 3], ["a", "b"]] empty_list = [] complex_list = [{"a": 1, "b": [1, 2]}, ["apple", "banana", "orange"]]

List Methods

  • Built-in functions that perform specific operations on lists and return a new list or modify the existing list
  • Examples:
    1. Position Related: Indexing, Slicing, index()
    2. Generic: Repetition, join
    3. Modification: append(), pop(), insert(), remove(), sort(), extend(), concat
    4. Numeric Functions: sum(), min(), max(), len(), count()

Indexing

  • Used for accessing individual elements in a list based on position
  • Starts from 0, Negative indexing is also allowed
  • Examples:
    1. my_list = [10, 20, 30, 40, 50] first_element = my_list[0] print(first_element) # 10 print(my_list[2]) # 30 print(my_list[-1]) # 50 my_list[1] = 25 print(my_list) # [10, 25, 30, 40, 50] i.e. mutable two_d_list = [[1, 2], ["cat", "dog"], [5, 6]] print(two_d_list[0][1]) # 2 (accessing first element first and then its second item)

Slicing

  • Extracts a sub-list from a items of list based on position
  • Starts from 0. Negative indexing is also allowed
  • Examples:
    1. my_list = [10, 20, 30, 40, 50] sub_list = my_list[1:4] print(sub_list) # [20, 30, 40] print(my_list[0:5:2]) # [10, 30, 50] print(my_list[:3]) # [10, 20, 30] print(my_list[2:]) # [30, 40, 50] print(my_list[::-1]) # [50, 40, 30, 20, 10]

index

  • Finds the position of the first occurrence of a specified value in a list
  • Raises ValueError if the value is not found in the list
  • Doesn't support find function like strings
  • Examples:
    1. my_list = [10, 20, 30, 40, 20, 50] index_of_30 = my_list.index(30) print(index_of_30) # 2 print(my_list.index(20)) # 1 print(my_list.index(60)) # Raises ValueError

Repetition

  • Used for repeating items of a list multiple times n times using * operator
  • Examples:
    1. my_list = [1, 2, 3] repeated_list = my_list * 3 print(repeated_list) # [1, 2, 3, 1, 2, 3, 1, 2, 3] print([0] * 5) # [0, 0, 0, 0, 0] print(["a", "b"] * 4) # ["a", "b", "a", "b", "a", "b", "a", "b"]

join

  • Used to join a list of strings into a single string with a specified delimiter
  • Examples:
    1. str_list = ["Hello", "World"] joined_str = " ".join(str_list) print(joined_str) # "Hello World" print(type(joined_str)) # <class 'str'> print("-".join(["2026", "03", "01"])) # "2026-03-01" print(",".join(["apple", "banana", "orange"])) # "apple,banana,orange" print(".".join("abc")) # "a.b.c"

append

  • Used for adding an element to the end of a list
  • In-place operation that modifies the original list
  • Examples:
    1. my_list = [1, 2, 3] my_list.append(4) print(my_list) # [1, 2, 3, 4] my_list.append([5, 6]) print(my_list) # [1, 2, 3, 4, [5, 6]]

pop

  • Removes and returns an element from a list at a specified index (default is last)
  • Combining append and pop, we can implement stack data structure (LIFO)
  • Examples:
    1. my_list = [10, 20, 30, 40, 50] last_element = my_list.pop() print(last_element) # 50 print(my_list) # [10, 20, 30, 40] second_element = my_list.pop(1) print(second_element) # 20 print(my_list) # [10, 30, 40]

insert

  • Used for adding an element at a specified index in a list
  • Shifts other elements to right
  • Examples:
    1. my_list = [1, 2, 4] my_list.insert(2, 3) print(my_list) # [1, 2, 3, 4] my_list.insert(0, 0) print(my_list) # [0, 1, 2, 3, 4] my_list.insert(10, 5) print(my_list) # [0, 1, 2, 3, 4, 5] i.e. if index is greater than list length, it appends to the end

remove

  • Used to remove the first occurrence of a specified value from a list
  • Shifts other elements to left
  • Raises ValueError if the value is not found in the list
  • Examples:
    1. my_list = [1, 2, 3, 4, 2] my_list.remove(2) print(my_list) # [1, 3, 4, 2] (only first occurrence of 2 is removed) my_list.remove(5) # Raises ValueError

sort and sorted

  • sort and sorted sorts the elements of a list in ascending order by default
  • Examples:
    1. my_list = [3, 1, 4, 2] my_list.sort() print(my_list) # [1, 2, 3, 4] my_list = [3, 1, 4, 2] sorted_list = sorted(my_list) print(sorted_list) # [1, 2, 3, 4] print(my_list) # [3, 1, 4, 2] (original list remains unchanged) str_list = ["apple", "ball", "cat"] str_list.sort(reverse=True) print(str_list) # ['cat', 'ball', 'apple'] (sort in descending order) str_list.sort(key=len) print(str_list) # ['ball', 'cat', 'apple'] (sort by length of strings)

extend and concat

  • extend is used to add all elements of an iterable (like list, tuple, etc.) to the end of the list
  • concat is used to combine two or more lists into a single list
  • Examples:
    1. list_1 = [1, 2, 3] list_2 = [4, 5, 6] list_1.extend(list_2) print(list_1) # [1, 2, 3, 4, 5, 6] list_3 = list_1 + list_2 print(list_3) # [1, 2, 3, 4, 5, 6, 4, 5, 6]

Copying Lists

  • Used to create a copy of a list to avoid modifying the original list when changes are made to the copy
  • Examples:
    1. original_list = [1, 2, 3] copied_list = original_list.copy() copied_list.append(4) print(original_list) # [1, 2, 3] print(copied_list) # [1, 2, 3, 4] another_copy = list(original_list) # or another_copy = original_list[:] another_copy.append(5) print(original_list) # [1, 2, 3] print(another_copy) # [1, 2, 3, 5]

Numeric Functions

  • Used to perform numeric operations on lists containing numbers
  • Valid Functions: sum (only for int and float lists), min, max, len, count
  • Examples:
    1. num_list = [1, 2, 3, 4, 5] print(sum(num_list)) # 15 print(min(num_list)) # 1 print(max(num_list)) # 5 print(len(num_list)) # 5 print(num_list.count(2)) # 1 print(num_list.count(10)) # 0