Python Concepts/Lists

Objective

 * Learn about Python lists.
 * Learn about list indexing.
 * Learn about list slicing.
 * Learn how to manipulate a list dynamically.
 * Learn about built-in list functions and methods.
 * Learn when to use lists and when not to.

Python Lists
Lists are an important data type that allows you to keep a set of iterable item(s). Lists are mutable sequences. This literally means you can have a list of items, like 1, 2, 3 or "h", "e", "l", "l", "o", "!". A list is denoted by square brackets,. To create a list that holds actual items, put them within square brackets and use commas to separate the items.

The built-in function,  converts a non-list into a list:

Lists can also hold multiple different data types, so you can mix numbers, strings, and complexes. Better yet, we can nest lists within each other.

A list can contain a two-dimensional array:

A list can contain a three-dimensional array:

List nesting can get very complicated as the nesting gets deeper, but it creates a great amount of power that will be harnessed in the later parts of this course.

List Indexing
Like strings, lists can be indexed. Indexing can be used to get one item out of the list. For example, if you want to get  out of   type   although, for brevity, we'll put the lists in a variable first. Don't forget that indexing is zero-based in Python.

Like strings, you can index starting with the last item by using a negative number. Don't forget that negative one is the start of the last item, not zero!

If you index out of range, by trying to get an item that doesn't exist, you'll get an.

List Slicing
Like strings, lists can be sliced. When you slice a list, it will return a new sliced copy of the original list.

Lists also support extended slicing, where the third parameter acts as the "step". A step of 1 is the default value.

List manipulation
Adding a new item to a list is pretty basic. The best way to add a new item is by using the built-in list method. This can be easily done as shown below. also:

Unlike strings, lists are mutable (can be changed). The best way to change an item in a list is to do so by indexing.

There are a few ways to remove an item from a list, but because Python culture dictates that there should be one preferable way to do something, I'll give an example that's fast and uses less code.

You should notice that when you delete an item, the items to the right of the deleted item move over to the left (if possible). So you could keep deleting item zero to remove all of the items. An easier way to delete a whole list is to follow the example with. This completely deletes all of the items in the list.

As stated earlier, lists are mutable types, which means their content can dynamically change. Take  for example. Spam really isn't the list, it just points to the location of the list. Take an address book for another example. It contains the location of a house. It doesn't know what's in the house, it just knows where the house is. and the list have the same relationship as the address book and the house.

Now this leads to some important questions, primarily how would you copy  if it contains the address of the list? Since  contains the address, giving another variable the same address isn't going to help you.

As you can see from the above example,  and   both point to the same list and any change to one affects the other. This is called a shallow copy. If you want two completely different lists, then you create a deep copy. When slicing, the return is always a deep copy, so we could perform a simple slice to create a new list for.

This is an important concept to remember early on so you don't run into any problems in the future. While the above example is accurate, it seems to be true only for small lists. NB the following:

Deep copy
See the reference: " Shallow and deep copy operations"

Built-in List Methods
While the following methods have the appearance of functions and it can be tempting to call them "functions," the correct description is "methods."

Adds item  to the end of list. equivalent to:

Removes all items from list. equivalent to:

Returns the number of times a given item  is found in list

Extend list by appending all items from iterable equivalent to:

Finds the first zero-based index of a given item  in list. The square brackets around  indicate that these two arguments are optional. start and end are interpreted as in slice notation and are used to limit the search to a particular subsequence of the list.

Case1:

Case2:  equivalent to

Case3:  equivalent to

Inserts item  at position   where   is the zero-based index of the element before which to insert:

equivalent to : or:

Out of range:

Returns  then deletes   If optional argument   is not provided   defaults to

equivalent to:

equivalent to:

Remove the first item from  whose value is   It is an error if there is no such item.

This reverses the order of list in place. equivalent to:

List Usage
Although lists are useful, they are about four to seven times slower than tuples, because lists aren't as static as tuples. Large lists usually have a higher penalty on performance, so try keep them short. As a general rule of thumb, use lists only when you need to dynamically hold information and values.

Using Lists as Stacks
The list methods make it easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use. To retrieve an item from the top of the stack, use  without an explicit index. For example:

Using Lists as Queues
While lists are not efficient for this purpose, they can be used to illustrate the concept quite well. A queue is a sequence of elements where the first element added is the first element retrieved (“first-in, first-out”).

List Comprehensions
List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element satisfies a certain condition applied to each element of a given list.

Given:

List all of the negative numbers in list a: The above is equivalent to:

How many floats are there in list a?

List the position and value of all odd numbers in list a:

Assignments

 * Work with some lists. Try creating a list of lists. How could this be used in a real life situation?
 * Work with some of the list's methods. Try them all out at least three times.
 * Does Python lists resemble real life lists? Could it be made into a simple grocery or to do list?

Further Reading or Review

 * Previous Lesson: Strings
 * This Lesson:Lists
 * Next Lesson: Bytes objects and Bytearrays
 * Course Home Page