Loading
Loading
Loading
Loading
Loading
Introduction to Lists
OverviewThe code you wrote above is great. But with a degree from the U of I, you will likely get more than one job offer. Assume you get three offers. Can you reuse the logic you've already written? Yes! But we'll first need to cover a new data type (list
) and a new syntax (for
loops).
Last time, we talked about bool
, int
, float
, and str
types. Variables with these three types can only contain a single value. That's why they are called basic types. The list
type is the first non-basic data type we've seen. Square brackets ([]
) are used to denote a list type.
Loading
Loading
Loading
Accessing list elements by index
Zero-based IndexWhat does that offers[0]
do? The square brackets used in offers[0]
denote the index of the element you're trying to retrieve. offers[0]
retrieves the first value in the array, which is 55000
in our code. But why does the index start at zero, not one?
In many programming languages including Python, you start counting indices at zero, not one. To retrieve the second item from my_list
, the correct code would be my_list[1]
, not my_list[2]
.
Loading
Loading
Loading
Loading
Negative list indices
From the end of the listYou can also access elements from the end of the array using a negative index. Negative indices begin at -1
. my_list[-1]
will select the last element in my_list
.
Similarly, my_list[-2]
will select the second to the last element in my_list
.
Loading
Loading
Loading
Loading
How many elements are in a list?
Length of a listYou can retrieve the number of items in a list by using len(my_list)
syntax.
Loading
Loading
Loading
Working with lists
List methodsPython provides multiple list methods (if you're not familiar with what a "method" is, think of it as some operation on a list - like adding or removing an item).
A common operation is to add an item to an existing list. You can append an item to a list using my_list.append(new_value)
syntax.
If you'd like to take a look at all available methods, refer to https://docs.python.org/3/tutorial/datastructures.html .
Loading
Can a list have elements with non-uniform data types?
Mixed TypesFrom the previous coding question, we're able to see that a list can have elements with mixed data types. A list can hold any data type. In fact, a list can hold another list, which is called a nested list. For now, let's keep it simple and not worry about nested lists.
A list can contain a list that contains another list that contains another list... and so on.
Loading
Loading
Do we really want to keep copy-pasting stuff?
Repetition is never goodWell that's a little inefficient, isn't it? You may be okay with copy-pasting the print(roster[n])
code a couple of times. But imagine if the roster
list had 1,000 names. You'd be spending your entire afternoon copy-pasting and changing numbers.
Loading
Loading
Loading
Loading
Loading
Another way to retrieve elements
A second syntaxYou can also use for x in my_list
to iterate over each value in my_list
. Note that x
is an arbitrary name you pick. You can use any name. Any of the code below will work.
for x in my_list
for a in my_list
for my_value in my_list
for any_variable_name in my_list