Lists in Python
In Python, a list is a collection of values, which can be of different data types. Lists are one of the most commonly used data structures in Python, and they offer a variety of useful methods for working with data.
Creating a List
You can create a list by enclosing a comma-separated sequence of values in square brackets:
# Example of creating a list
fruits = ['apple', 'banana', 'orange']
print(fruits) # Output: ['apple', 'banana', 'orange']
You can also create an empty list by using empty square brackets:
# Example of creating an empty list
my_list = []
print(my_list) # Output: []
Accessing List Items
You can access individual items in a list by using their index. The index of the first item in a list is
0
, the index of the second item is
1
, and so on.
# Example of accessing list items
fruits = ['apple', 'banana', 'orange']
print(fruits[0]) # Output: 'apple'
print(fruits[1]) # Output: 'banana'
print(fruits[2]) # Output: 'orange'
You can also use negative indexing to access items from the end of the list:
# Example of negative indexing
fruits = ['apple', 'banana', 'orange']
print(fruits[-1]) # Output: 'orange'
print(fruits[-2]) # Output: 'banana'
print(fruits[-3]) # Output: 'apple'
Modifying List Items
You can modify individual items in a list by assigning a new value to their index:
# Example of modifying list items
fruits = ['apple', 'banana', 'orange']
fruits[1] = 'pear'
print(fruits) # Output: ['apple', 'pear', 'orange']
Adding Items to a List
You can add new items to the end of a list using the
append()
method:
# Example of adding items to a list
fruits = ['apple', 'banana', 'orange']
fruits.append('pear')
print(fruits) # Output: ['apple', 'banana', 'orange', 'pear']
You can also insert new items into a list at a specific index using the
insert()
method:
# Example of inserting items into a list
fruits = ['apple', 'banana', 'orange']
fruits.insert(1, 'pear')
print(fruits) # Output: ['apple', 'pear', 'banana', 'orange']
Removing Items from a List
You can remove an item from a list by using the
remove()
method and specifying the value to be removed:
# Example of removing an item from a list
fruits = ['apple', 'banana', 'orange']
fruits.remove('banana')
print(fruits) # Output: ['apple', 'orange']
You can also remove the last item from a list using the
pop()
method:
# Example of removing the last item from a list
fruits = ['apple', 'banana', 'orange']
fruits.pop()
print(fruits) # Output: ['apple', 'banana']
List Slicing
You can extract a portion of a list by using slicing. Slicing allows you to specify a range of indexes to include in the new list. The range is specified by two indexes separated by a colon
:
.
The first index is the starting index and the second index is the ending index. The slice will include all items from the starting index up to, but not including, the ending index. If you omit the first index, the slice will start from the beginning of the list. If you omit the second index, the slice will include all items up to the end of the list.
# Example of list slicing
fruits = ['apple', 'banana', 'orange', 'pear', 'grape']
print(fruits[1:3]) # Output: ['banana', 'orange']
print(fruits[:2]) # Output: ['apple', 'banana']
print(fruits[3:]) # Output: ['pear', 'grape']
Iterating Over a List
You can use a
for
loop to iterate over the items in a list:
# Example of iterating over a list
fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
print(fruit)
This will output each item in the list on a new line:
apple
banana
orange
List length
You can get the number of elements in a list using the len() function.
For example, to get the number of elements in the fruits list, you can use:
print(len(fruits))
# This will output 3.
List Comprehensions
List comprehensions provide a concise way to create lists based on existing lists. They consist of an expression followed by a
for
clause, and optionally one or more
if
clauses. The result will be a new list based on the expression and the conditions specified in the
for
and
if
clauses. We'll learn more about it later
Conclusion
Lists are a fundamental data structure in Python. They are easy to use, versatile, and widely used. By mastering lists, you'll be well on your way to becoming a proficient Python programmer.