Python Lists
Python Lists
List Variable
+
Variables vs. Lists
+
Variables vs. Lists
+
Lists in Python
my_list = [1, 2, 3]
n The above code will create a new list in Python that holds
three integers – 1, 2 and 3 – in that order.
n Lists can contain any data type that we have covered so far.
Example:
n You can print the value of a list using the print() function.
Example:
print (my_list)
+
List Repetition
my_list = [1, 2, 3] * 3
print (my_list)
>> [1, 2, 3, 1, 2, 3, 1, 2, 3]
+
List Concatenation
n Example:
n Example:
mylist1 = [1,2,3]
mylist2 = mylist1
print (mylist1)
print (mylist2)
>> [1,2,3]
>> [1,2,3]
+
List Mechanics
n This means that you can change one of the lists and the
change will be reflected in the other.
mylist1 = [1,2,3]
mylist2 = mylist1
mylist1[0] = 999
print (mylist1)
print (mylist2)
>> [999,2,3]
>> [999,2,3]
+
Copying a List
n Python will only create new lists when you use [] syntax to define
a list for the first time
mylist = []
mylist = [0] * 10
+
Creating Lists
n You can also create lists using the range() function. For
example, to create a list of all even numbers between 0 and
100 you can do the following:
even_numbers = list(range(0,100,2))
+
Iterating over a list
+
Using a “for” loop to iterate
through a List
n You can also use a for loop to iterate through a list. When you
do this the target variable of your loop assumes each value of
each element of the list in order. Example:
my_list = [1,2,3]
for number in my_list:
print (number)
>> 1
>> 2
>> 3
+
Programming Challenge: Count
the A’s
n Given the following list:
grades = [90,100,70,45,76,84,93,21,36,99,100]
n The target variable in a for loop assumes the value of the current item in
the list as you iterate.
n However, the target variable isn’t very helpful if you want to change the
value of an item in a list since it is just a copy of the data that exists in the
list. For example:
mylist = [1,2,3]
for n in mylist:
n = n * 5
print (mylist)
>> [1,2,3]
+
Changing List Items
mylist = [1,2,3]
mylist[0] = 999
print (mylist)
>> [999, 2, 3]
+
Iterating over a list using index
values
n There are two main techniques for iterating over a list using
index values. They include:
n Setting up a counter variable outside the list and continually
updating the variable as you move to the next position in the list
n Using the range() function to create a custom range that
represents the size of your list
+
Using a counter variable and a for
loop to iterate over a list
n If you set up an accumulator variable outside of your loop you
can use it to keep track of where you are in a list. For
example:
mylist = [1,2,3]
counter = 0
for num in mylist:
mylist[counter] = mylist[counter] * 2
counter += 1
print (mylist)
>> [2,4,6]
+
Using the range() function to
iterate over a list
n You can also use the range() function to construct a custom
range that represents all of the indexes in a list. Example:
mylist = [1,2,3]
for counter in range(0,len(mylist)):
mylist[counter] = mylist[counter] * 2
print (mylist)
>> [2,4,6]
+
Programming Challenge
n Example:
n Python will copy out the elements from the list on the right
side of the assignment operator based on the start and end
indexes provided.
n Note that indexes work just like the range() function – you
will grab items up until the end index, but you will not grab
the end index itself
+
Slicing Lists
n You can easily find a particular item in a list by using the “in”
operator. Here’s an example:
n The “in” operator lets you search for any item in a list. It will
return a Boolean value that indicates whether the item exists
somewhere in the list.
+
Programming Challenge
n Given the following lists, write a program that lets the user
type in a product code. If the product name exists in our
inventory you should print out that it is in our inventory.
Otherwise you should print out that the product is not found.
a = [1,2,3,4,5]
b = [2,3,10,11,12,1]
n Write a program that finds all elements that exist in both lists
(i.e. the integer 2 exists in both lists). Store your result in a
list and print it out to the user.
+
Adding items to a list
n You have already seen a few ways in which you can add items to
lists:
n Repeat the list using the “*” operator
n Concatenate the list using the “+” operator
n You can have Python sort items in a list using the sort()
method. Here’s an example:
n Caution: The index method will throw an exception if it cannot find the
item in the list.
n Here’s an example:
n Given that the following lists match up with one another (i.e.
the product at the first position of the products list matches
the price at the first position in the prices list), write a
product price lookup program.
n Note that you will raise an exception if you try and remove
something that is not in the list. Make sure to test to see if it is
in the list first using the “in” operator, or use a try / except
block to catch any errors you might raise.
+
Removing items from a list
n You can also remove an item from a list based on its index
position. We can do this using the 'del' keyword, like this:
n This will not sort the list in reverse order – it will simply
shuffle the elements of a list such that the first element
becomes the last element, the second element becomes the
second to last element, etc.
+ Programming Problems
+
Programming Challenge