Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In this Python List tutorial, we'll cover the basics of Python lists, including how to create, access, modify, and remove elements from a list. We'll also discuss some built-in list methods and common operations.
A list is an ordered collection of elements, which can be of any data type. Lists are mutable, which means you can modify their contents. To create a list, use square brackets []
and separate elements with commas.
my_list = [1, 2, 3, "apple", 4.5, True]
List elements can be accessed using indices, which start at 0 for the first element, 1 for the second element, and so on. Negative indices can be used to access elements from the end of the list.
my_list = [1, 2, 3, 4, 5] print(my_list[0]) # Output: 1 print(my_list[2]) # Output: 3 print(my_list[-1]) # Output: 5
List elements can be modified using their indices:
my_list = [1, 2, 3, 4, 5] my_list[1] = 20 print(my_list) # Output: [1, 20, 3, 4, 5]
You can remove list elements using the remove()
, pop()
, or del
methods:
my_list = [1, 2, 3, 4, 5] # Using remove() my_list.remove(3) print(my_list) # Output: [1, 2, 4, 5] # Using pop() my_list.pop(1) print(my_list) # Output: [1, 4, 5] # Using del del my_list[0] print(my_list) # Output: [4, 5]
List slicing allows you to create a new list from a portion of an existing list:
my_list = [1, 2, 3, 4, 5] sliced_list = my_list[1:4] print(sliced_list) # Output: [2, 3, 4]
Python has several built-in list methods, such as:
append()
: Adds an element to the end of the list.extend()
: Adds multiple elements to the end of the list.insert()
: Inserts an element at the specified index.count()
: Returns the number of occurrences of the specified element.index()
: Returns the index of the first occurrence of the specified element.sort()
: Sorts the list in ascending order (or using a custom comparison function).reverse()
: Reverses the order of the list.How to create a list in Python:
my_list = [1, 2, 3, 4, 5]
Python list methods and functions:
my_list = [6, 7, 8, 9, 10] length = len(my_list) maximum_value = max(my_list) my_list.append(11)
Accessing elements in a Python list:
my_list = ['apple', 'orange', 'banana'] first_element = my_list[0] third_element = my_list[2]
Adding elements to a Python list:
append
, insert
, or the +
operator.my_list = [11, 12, 13] my_list.append(14) my_list.insert(1, 15) new_elements = [16, 17] my_list += new_elements
Removing items from a Python list:
remove
, pop
, or through slicing.my_list = [18, 19, 20, 19] my_list.remove(19) popped_item = my_list.pop(1) del my_list[0]
Iterating through a Python list:
for
loop or list comprehension.my_list = ['cat', 'dog', 'rabbit'] for animal in my_list: print(animal)
Common operations with Python lists:
my_list = [21, 22, 23, 24, 25] sublist = my_list[1:4] reversed_list = list(reversed(my_list)) sorted_list = sorted(my_list) is_present = 23 in my_list
List comprehension in Python:
my_list = [26, 27, 28, 29, 30] squared_values = [x**2 for x in my_list]