Smart Ways to Utilize Lists
Since lists are a type of data structure, you can apply various operations to process data efficiently.
For instance, applying the + operation
to two lists will merge them into one, and applying the x operation
to a list will repeat the list by that number of times.
In this lesson, we will explore commonly used operations and methods (functions that perform specific tasks) on lists.
List Addition
Adding two lists will concatenate them into a single list.
# List Addition
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# Combine two lists into the variable 'combined'
combined = list1 + list2
# Output: [1, 2, 3, 4, 5, 6]
print(combined)
List Multiplication
When you multiply a list by an integer, it creates a new list with the original list repeated that many times.
# List Multiplication
numbers = [0, 1, 2]
# Repeat the list 2 times and store in 'multiplied'
multiplied = numbers * 2
# Output: [0, 1, 2, 0, 1, 2]
print(multiplied)
Getting the Length of a List
To find out how many elements are in a list, use the len()
function.
fruits = ["apple", "banana", "cherry"]
# Store the length of the list in the variable 'length'
length = len(fruits)
print(length) # Output: 3
Finding the Minimum and Maximum Values in a List
To find the smallest or largest values in a list, use the min()
and max()
functions.
# Finding the Minimum Value
numbers = [5, 2, 9, 1, 7]
# Store the minimum value in the variable 'min_value'
min_value = min(numbers)
print(min_value) # Output: 1
# Store the maximum value in the variable 'max_value'
max_value = max(numbers)
print(max_value) # Output: 9
Calculating the Sum of List Elements
To sum the elements of a list of numbers, use the sum()
function.
# Calculating the Sum of List Elements
expenses = [250, 150, 75, 300]
# Store the sum of the list in the variable 'total'
total = sum(expenses)
print(total) # Output: 775
Checking for the Presence of an Element in a List
To verify if a list contains a certain value, use the in
operator.
The in
operator returns a boolean value of True
or False
.
# Checking for the Presence of an Element in a List
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # Output: True
print("orange" in fruits) # Output: False
Finding the Index of a Specific Value in a List
To find the position of a specific value in a list, use the index()
method.
This method returns the position (index) where the value first appears.
# Finding the Index of a Specific Value in a List
fruits = ["apple", "banana", "cherry", "banana"]
index = fruits.index("banana")
print(index) # Output: 1
The first occurrence of "banana" is the second element, but since indexing starts at 0, it prints 1.
Sorting a List
To sort a list in ascending or descending order, use the sort()
method.
Add the reverse=True
option to sort the elements in descending order.
# Sorting a List in Ascending Order
numbers = [5, 2, 9, 1, 7]
numbers.sort()
print(numbers) # Output: [1, 2, 5, 7, 9]
# Sorting a List in Descending Order
numbers.sort(reverse=True)
print(numbers) # Output: [9, 7, 5, 2, 1]
Note: The
sort()
method modifies the original list and does not return a new list. To maintain the original list, use thesorted()
function.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.