Chapter 3: (Continued)
Adding elements to a list
- you might want to add a new element to a list
- for example, add new data to a visualization or add new registered users to a website you’ve built
- Python provides several ways of adding new data to lists
Appending elements to the end of a list
- append – simplest way to add a new element to a list
- added to the end of the list
motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’]
print(motorcycles)
motorcycles.append(‘ducati’)
print(motorcycles)
[‘honda’, ‘yamaha’, ‘suzuki’]
[‘honda’, ‘yamaha’, ‘suzuki’, ‘ducati’]
- can start with an empty list and build it dynamically
motorcycles = []
motorcycles.append(‘honda’)
motorcycles.append(‘yamaha’)
motorcycles.append(‘suzuki’)
print(motorcycles)
[‘honda’, ‘yamaha’, ‘suzuki’]
Inserting elements into a list
- insert() method = add a new element at any position by specifying the index of the new element and the value of the new item
motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’]
motorycles.insert(0,’ducati’)
print(motorcycles)
[‘ducati’, ‘honda’, ‘yamaha’, ‘suzuki’]
Removing elements from a list
- often times you’ll want to remove an item
- del statement – remove an item from a list
motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’]
print(motorcycles)
del motorcycles[0]
print(motorcycles)
[‘honda’, ‘yamaha’, ‘suzuki’]
[‘yamaha’, ‘suzuki’]
Removing an item using the pop() method
- pop() method – removes the last item in a list, but it lets you work with that item after removing it
motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’]
print(motorcycles)
popped_motorcycle = motorcycles.pop()
print(motorcycles)
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
[‘honda’, ‘yamaha’, ‘suzuki’]
[‘honda’, ‘yamaha’]
suzuki
- for example, you had a list of motorcycles stored in chronological order according to when we owned them and you used the pop() method to print a statement about the last motorcycle we bought
motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’]
last_owned = motorcycles.pop()
print(f”The last motorcycle I owned was a {last_owned.title()}.”)
The last motorcycle I owned was a Suzuki.
Popping items from any position in a list
- pop() can remove an item from any position with indexing
motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’]
first_owned = motorcycles.pop(0)
print(f”The first motorcycle I owned was a {first_owned.title()}.”)
The first motorcycle I owned was a Honda.
Removing an item by value
- remove () method – used to remove an item by value, not position
- for example, we want to remove the value ‘ducati’
motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’, ‘ducati’]
print(motorcycles)
motorcycles.remove(‘ducati’)
print(motorcycles)
[‘honda’, ‘yamaha’, ‘suzuki’ ‘ducati’]
[‘honda’, ‘yamaha’, ‘suzuki’]
- can also remove() method to work with a value that’s being removed from a list
- let’s remove the value ‘ducati’ and print a reason for removing it from the list
motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’ ‘ducati’]
print(motorcycles)
too_expensive = ‘ducati’
motorcycles.remove(too_expensive)
print(motorcycles)
print(f’\nA {too_expensive.title()} is too expensive for me.”)
[‘honda’, ‘yamaha’, ‘suzuki’, ‘ducati’]
[‘honda’, ‘yamaha’, ‘suzuki’]
A Ducati is too expensive for me.
Organizing a list
- Python provides a number of different ways to organize lists
Sorting a list permanently with the sort() method
- sort() method makes it easy to sort a list alphabetically
cars.py
cars = [‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]
cars.sort()
print(cars)
[‘audi’, ‘bmw’, ‘subaru’, ‘toyota’]
Pass argument reverse=True to sort() method to sort list in reverse-alphabetical order
cars = [‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]
cars.sort(reverse=True)
print(cars)
[‘toyota’, ‘subaru’, ‘bmw’, ‘audi’]
Sorting a list temporarily with the sorted() function
- sorted() function – displays a list in a particular order without affecting the actual order of the list
End of study session.