Back to Course

## Intermediate Python

0% Complete
0/0 Steps

#### Participants 676

Lesson 3 of 33
In Progress

##### Yasin Cakal

n this lesson, we will cover advanced techniques for working with lists in Python. This includes techniques for sorting, searching, and manipulating lists in various ways.

## Sorting Lists:

In Python, you can use the sorted() function to sort a list. By default, this function will sort the list in ascending order. You can also specify a key function to use for sorting, as well as whether to sort the list in ascending or descending order.

For example, to sort a list of integers in ascending order, you can do the following:

my_list = [3, 6, 1, 9, 2]
sorted_list = sorted(my_list)
print(sorted_list)  # Output: [1, 2, 3, 6, 9]


You can also use the key parameter to specify a function to use for sorting. This function should take a single argument and return a value that will be used for comparison. For example, to sort a list of strings by their length, you can do the following:

my_list = ['apple', 'banana', 'cherry', 'date']
sorted_list = sorted(my_list, key=len)
print(sorted_list)  # Output: ['date', 'apple', 'banana', 'cherry']


You can also use the reverse parameter to specify whether to sort the list in ascending or descending order. For example, to sort a list of integers in descending order, you can do the following:

my_list = [3, 6, 1, 9, 2]
sorted_list = sorted(my_list, reverse=True)
print(sorted_list)  # Output: [9, 6, 3, 2, 1]


## Searching Lists:

In Python, you can use the in operator to check if an element is in a list. For example:

my_list = [1, 2, 3, 4, 5]
print(2 in my_list)  # Output: True
print(6 in my_list)  # Output: False


You can also use the index() method to find the index of an element in a list. If the element is not in the list, this method will raise a ValueError. For example:

my_list = [1, 2, 3, 4, 5]
print(my_list.index(3))  # Output: 2
print(my_list.index(6))  # Output: ValueError: 6 is not in list


## Manipulating Lists:

In Python, you can use the append() method to add an element to the end of a list. For example:

my_list = [1, 2, 3, 4, 5]
my_list.append(6)
print(my_list)  # Output: [1, 2, 3, 4, 5, 6]


You can also use the insert() method to insert an element at a specific index in a list. For example:

my_list = [1, 2, 3, 4, 5]
my_list.insert(2, 6)
print(my_list)  # Output: [1, 2, 6, 3, 4, 5]


You can use the remove method to remove elements from a list. This method will remove the first occurrence of the element you specify. If the element is not found in the list, it will raise a ValueError exception.

You can use the pop method to remove elements from a list and return the element that was removed. If you don’t specify an index, pop will remove and return the last element in the list. You can also specify an index to remove and return a specific element.

The del statement can be used to remove elements from a list by specifying the index or a slice of indexes.

You can use the sort method to sort a list in ascending order. You can also specify the reverse parameter as True to sort the list in descending order.

You can use the sorted function to return a new sorted list from an iterable, without modifying the original iterable.

You can use the reverse method to reverse the order of the elements in a list.

You can use the count method to count the number of occurrences of an element in a list.

You can use the index method to find the index of the first occurrence of an element in a list. You can also specify a start and stop index to search within a range of the list. If the element is not found, it will raise a ValueError exception.

## Conclusion

In summary, there are many advanced operations you can perform on lists in Python, including removing elements, sorting, reversing, and searching.

## Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

#### Write a function that takes a list of numbers as an argument and returns a list of the numbers that are divisible by 3.

def divisible_by_3(nums):
return [x for x in nums if x % 3 == 0]

print(divisible_by_3([1, 2, 3, 4, 5, 6])) # [3, 6]
print(divisible_by_3([10, 11, 12, 13, 14, 15])) # [12, 15]


#### Write a function that takes a list of strings as an argument and returns a list of the strings that are palindromes.

def palindrome_strings(strings):
return [x for x in strings if x == x[::-1]]

print(palindrome_strings(['racecar', 'hello', 'world', 'level'])) # ['racecar', 'level']
print(palindrome_strings(['abc', 'def', 'ghi', 'cba'])) # ['abc', 'cba']


#### Write a function that takes a list of integers as an argument and returns the sum of the even numbers in the list.

def sum_even_numbers(numbers):
return sum([x for x in numbers if x % 2 == 0])

print(sum_even_numbers([1, 2, 3, 4, 5, 6])) # 12
print(sum_even_numbers([10, 11, 12, 13, 14, 15])) # 46


#### Write a function that takes a list of integers as an argument and returns the product of the odd numbers in the list.

def product_odd_numbers(numbers):
product = 1
for x in numbers:
if x % 2 != 0:
product *= x
return product

print(product_odd_numbers([1, 2, 3, 4, 5, 6])) # 15
print(product_odd_numbers([10, 11, 12, 13, 14, 15])) # 11 * 13 * 15 = 2145


#### Write a function that takes a list of integers as an argument and returns a list of the integers that are prime numbers.

def prime_numbers(numbers):
def is_prime(num):
if num < 2:
return False
for i in range(2, num):
if num % i == 0:
return False
return True

return [x for x in numbers if is_prime(x)]

print(prime_numbers([1, 2, 3, 4, 5, 6])) # [2, 3, 5]
print(prime_numbers([10, 11, 12, 13, 14, 15])) # [11, 13]