Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In Python, lists are ordered, mutable collections of items. Lists can store items of different data types, such as integers, strings, and other objects. Here's a tutorial on working with lists in Python:
Creating a list:
You can create a list by placing a comma-separated sequence of items inside square brackets []
.
my_list = [1, 2, 3, 4, 5] print(my_list) # Output: [1, 2, 3, 4, 5] mixed_list = [1, 'apple', 3.14, True] print(mixed_list) # Output: [1, 'apple', 3.14, True]
Accessing list items:
You can access items in a list by their index, starting with 0 for the first item. Negative indices can be used to access items from the end of the list.
my_list = ['apple', 'banana', 'cherry', 'orange'] print(my_list[1]) # Output: 'banana' print(my_list[-1]) # Output: 'orange'
Slicing lists:
You can extract a sublist from a list by specifying a start and end index. The start index is inclusive, and the end index is exclusive.
my_list = [0, 1, 2, 3, 4, 5] print(my_list[1:4]) # Output: [1, 2, 3] print(my_list[:3]) # Output: [0, 1, 2] print(my_list[2:]) # Output: [2, 3, 4, 5]
Modifying list items:
Lists are mutable, so you can change the value of an item by assigning a new value to its index.
my_list = ['apple', 'banana', 'cherry'] my_list[1] = 'grape' print(my_list) # Output: ['apple', 'grape', 'cherry']
Adding items to a list:
You can use the append()
method to add an item to the end of a list or the insert()
method to insert an item at a specific index.
my_list = ['apple', 'banana', 'cherry'] my_list.append('orange') print(my_list) # Output: ['apple', 'banana', 'cherry', 'orange'] my_list.insert(1, 'grape') print(my_list) # Output: ['apple', 'grape', 'banana', 'cherry', 'orange']
Removing items from a list:
You can use the remove()
method to remove an item by value or the pop()
method to remove an item by index. The pop()
method also returns the removed item.
my_list = ['apple', 'banana', 'cherry'] my_list.remove('banana') print(my_list) # Output: ['apple', 'cherry'] item = my_list.pop(0) print(item) # Output: 'apple' print(my_list) # Output: ['cherry']
Iterating over a list:
You can use a for
loop to iterate over the items in a list.
fruits = ['apple', 'banana', 'cherry'] for fruit in fruits: print(fruit)
How to Create a List in Python:
[]
to create a list with elements.# Example my_list = [1, 2, 3, 4, 5]
List Data Type in Python:
# Example mixed_list = [1, 'two', 3.0, [4, 5]]
Python List Methods and Operations:
append()
, extend()
, and remove()
.# Example my_list.append(6) # Add element to the end my_list.extend([7, 8]) # Extend with another list my_list.remove(3) # Remove element
Accessing and Modifying List Elements in Python:
# Example first_element = my_list[0] my_list[1] = 'two'
Iterating Through a List in Python:
for
) to iterate through list elements.# Example for element in my_list: print(element)
List Comprehension in Python:
# Example squared_numbers = [x**2 for x in range(1, 6)]
Sorting and Reversing Lists in Python:
sort()
for in-place sorting and sorted()
for creating a sorted copy.reverse()
method reverses the elements in-place.# Example my_list.sort() # In-place sorting sorted_list = sorted(my_list) # Create a sorted copy my_list.reverse() # In-place reversing
Slicing and Indexing in Python Lists:
# Example subset = my_list[1:4] # Slice from index 1 to 3
Common Operations on Python Lists:
# Example length = len(my_list) is_present = 2 in my_list count_2 = my_list.count(2)