A sequence is an object which can contain multiple items of data. We can perform operations on a sequence that allow us to work with those individual items.
A list, is a sequence in Python, that is mutable, meaning the data within it can be changed while the program is running. Each item in a list is called an element.
# examples of lists
my_num = [44, 45, 20, 16, 5]
names = ["Mike", "Christina", "Jeff", "Shannon"]
Notice how each element is separated by a comma. Also, elements in a list do not have to follow a given order. They can be any type of data, numeric, strings, or even types we haven’t looked at yet. In many languages, a list is known as an array, and the data has to be of the same type. However, in Python, you can mix data types within the list.
info = ["Gru", 42, "Agnus", 18, "Margaret", 21, "Edith", 28]
You can use the print command with a list, however, it will print the list, as you see it created, within square brackets, with commas between each element.
Making Lists
Python has a list function, list(), that will allow you to convert certain objects into lists. The most common is taking an iteratable object, like one created by range, to make it into a list.
data = list(range[5])
The repetition operator is another tool for making lists. It might seem like you are just doing multiplication, since it is also the *, but when you put a single element on the left side in square brackets, and a number on the right, it will create a list of the first element for the given number of times on the right.
data = [1] * 10
print(data)
If you use a list on the left side with more than one element, it will create that many lists.
data = [1,2,3] * 5
print(data)
Working with a List
One of the main things we need to do with a list is iterate over it. That basically means to go through each element, one at a time.
We went through a loop a few weeks ago, including a for loop over a collection of elements. Well, you can go through a list with a for loop.
#previous method
for num in [3,4,5,6] :
print(num)
# using a list
numbers = [3,4,5,6]
for num in numbers :
print(num)
The advantage to this is that you don’t have to type in those values. They can be pulled from an external source, inputed from the user, modified, etc. and your loop will still work as it was designed, but with inputs you don’t have to worry about.
Indexing is the process of picking a specific element to work with in a list. Each element in an list has a specific index, starting at 0 (zero) and working up to be n-1 where n is the number of elements in the list.
numbers = [3,4,5,6]
print(numbers[2]) #prints the 3rd element, 5
In Python, you can also use a negative index, to go backwards through your list. However, those start with -1, because 0 is already used.
numbers = [3,4,5,6]
print(numbers[-1]) #prints the 4th element, 6
You can use the index, and a while loop to go through a list, however it isn’t really recommended, especially if you need to go through the whole list. The for loop, a counting loop, is much better designed for that.
The len() is a function which returns the length of the array. You can access and use the value directly, or store it in a variable the size of your list isn’t going to change. This is preferred, especially if you will be using that value a lot as you won’t have to recalculate the length.
numbers = [3,4,5,6]
print(len(numbers))
# or
size = len(numbers)
print(size)
# here is where you might want to store the len in a variable so you don't have to keep calculating it.
numbers = [3,4,5,6]
index = 1
size = len(numbers)
for num in numbers :
print(num, "is the", index ,"element of", size, "in our list.")
Using Lists in Python was originally found on Access 2 Learn