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
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"]]
Examples:
List Methods
- Built-in functions that perform specific operations on lists and return a new list or modify the existing list
- Position Related: Indexing, Slicing,
index() - Generic: Repetition, join
- Modification:
append(),pop(),insert(),remove(),sort(),extend(),concat - Numeric Functions:
sum(),min(),max(),len(),count()
Examples:
Indexing
- Used for accessing individual elements in a list based on position
- Starts from 0, Negative indexing is also allowed
my_list = [10, 20, 30, 40, 50]first_element = my_list[0] print(first_element)# 10print(my_list[2])# 30print(my_list[-1])# 50my_list[1] = 25 print(my_list)# [10, 25, 30, 40, 50] i.e. mutabletwo_d_list = [[1, 2], ["cat", "dog"], [5, 6]]print(two_d_list[0][1])# 2 (accessing first element first and then its second item)
Examples:
Slicing
- Extracts a sub-list from a items of list based on position
- Starts from 0. Negative indexing is also allowed
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]
Examples:
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
findfunction like strings my_list = [10, 20, 30, 40, 20, 50]index_of_30 = my_list.index(30) print(index_of_30)# 2print(my_list.index(20))# 1print(my_list.index(60))# Raises ValueError
Examples:
Repetition
- Used for repeating items of a list multiple times n times using
*operator 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"]
Examples:
join
- Used to join a list of strings into a single string with a specified delimiter
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"
Examples:
append
- Used for adding an element to the end of a list
- In-place operation that modifies the original list
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]]
Examples:
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)
my_list = [10, 20, 30, 40, 50]last_element = my_list.pop() print(last_element)# 50print(my_list)# [10, 20, 30, 40]second_element = my_list.pop(1) print(second_element)# 20print(my_list)# [10, 30, 40]
Examples:
insert
- Used for adding an element at a specified index in a list
- Shifts other elements to right
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
Examples:
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
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
Examples:
sort and sorted
sortandsortedsorts the elements of a list in ascending order by defaultmy_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)
Examples:
extend and concat
extendis used to add all elements of an iterable (like list, tuple, etc.) to the end of the listconcatis used to combine two or more lists into a single listlist_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_2print(list_3)# [1, 2, 3, 4, 5, 6, 4, 5, 6]
Examples:
Copying Lists
- Used to create a copy of a list to avoid modifying the original list when changes are made to the copy
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)# oranother_copy = original_list[:]another_copy.append(5)print(original_list)# [1, 2, 3]print(another_copy)# [1, 2, 3, 5]
Examples:
Numeric Functions
- Used to perform numeric operations on lists containing numbers
- Valid Functions:
sum(only for int and float lists),min,max,len,count num_list = [1, 2, 3, 4, 5]print(sum(num_list))# 15print(min(num_list))# 1print(max(num_list))# 5print(len(num_list))# 5print(num_list.count(2))# 1print(num_list.count(10))# 0
Examples:
