In this blog and the next you’ll learn
what lists are and how to start working with
the elements in a list. Lists allow you to store
sets of information in one place, whether you
have just a few items or millions of items. Lists are
one of Python’s most powerful features readily accessible to new programmers, and they tie together many
important concepts in programming.
What is a List?
A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ] and individual elements in the list are separated by commas. Here’s a simple example of a list that
contains a few kinds of bicycles:
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles) gives ['trek', 'cannondale', 'redline', 'specialized']
Accessing Elements in a List
In order to access an element in a list, write the name of the list followed by the index of the item
enclosed in square brackets.
Note: Python considers the first item in a list to be at position 0, not position 1.
print(bicycles[0]) gives trek.
print(bicycles[2]) gives redline.
Using Individual Values from a List
message = "My first bicycle was a " + bicycles[0].title() + "."
print(message) gives My first bicycle was a Trek.
Changing, Adding, and Removing Elements
Most lists you create will be dynamic, meaning you’ll build a list and
then add and remove elements from it as your program runs its course.
Modifying Elements in a List
The syntax for modifying an element is similar to the syntax for accessing
an element in a list.
For example, let’s say we have a list of motorcycles, and the first item in
the list is 'honda'. How would we change the value of this first item?
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles) gives ['honda', 'yamaha', 'suzuki']
motorcycles[0] = 'ducati'
print(motorcycles) gives ['ducati', 'yamaha', 'suzuki']
Adding Elements to a List
Appending Elements to the End of a List
The simplest way to add a new element to a list is to append the item to the
list. When you append an item to a list, the new element is added to the end
of the list
motorcycles.append('ducati')
print(motorcycles) gives ['honda', 'yamaha', 'suzuki', 'ducati']
Inserting Elements into a List
You can add a new element at any position in your list by using the insert()
method. You do this by specifying the index of the new element and the
value of the new item.
motorcycles.insert(0, 'ducati')
print(motorcycles) gives ['ducati', 'honda', 'yamaha', 'suzuki']
Removing Elements from a List
Removing an Item Using the del Statement
If you know the position of the item you want to remove from a list, you can
use the del statement.
motorcycles = ['honda', 'yamaha', 'suzuki']
del motorcycles[0]
print(motorcycles) gives ['yamaha', 'suzuki']
Removing an Item Using the pop() Method
Sometimes you’ll want to use the value of an item after you remove it from a
list. The pop() method removes the last item in a list, but it lets you work
with that item after removing it.
popped_motorcycle = motorcycles.pop()
print(motorcycles) gives ['honda', 'yamaha']
print(popped_motorcycle) gives suzuki.
You can actually use pop() to remove an item in a list at any position by
including the index of the item you want to remove in parentheses.
popped_motorcycle = motorcycles.pop(0)
Removing an Item by Value
Sometimes you won’t know the position of the value you want to remove
from a list. If you only know the value of the item you want to remove, you
can use the remove() method.
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
motorcycles.remove('ducati')
print(motorcycles) gives ['honda', 'yamaha', 'suzuki']
Organizing a List
Sorting a List Permanently with the sort() Method
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars) gives ['audi', 'bmw', 'subaru', 'toyota']
You can also sort this list in reverse alphabetical order by passing the
argument reverse=True to the sort() method.
cars.sort(reverse=True)
print(cars) gives ['toyota', 'subaru', 'bmw', 'audi']
Sorting a List Temporarily with the sorted() Function
To maintain the original order of a list but present it in a sorted order, you
can use the sorted() function. The sorted() function lets you display your list
in a particular order but doesn’t affect the actual order of the list.
print(sorted(cars)) gives ['audi', 'bmw', 'subaru', 'toyota']
print(cars) gives ['bmw', 'audi', 'toyota', 'subaru']
Printing a List in Reverse Order
cars.reverse()
print(cars) gives ['subaru', 'toyota', 'audi', 'bmw']
The reverse() method changes the order of a list permanently, but you
can revert to the original order anytime by applying reverse() to the same
list a second time.
Finding the Length of a List
You can quickly find the length of a list by using the len() function
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(len(cars)) gives 4.
Note:
Keep in mind that whenever you want to access the last item in a list
you use the index -1. This will always work, even if your list has changed
size since the last time you accessed it.
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[-1]) gives suzuki.
Conclusion
In this blog you learned what lists are and how to work with the individual items in a list. You learned how to define a list and how to add and
remove elements. You learned to sort lists permanently and temporarily for
display purposes.
Post a Comment
Let's make it better!
Comment your thoughts...