# Python Programming/Lists

This lesson introduces Python lists.

## Objectives and Skills

Objectives and skills for this lesson include:
* Data Structures
** List

## Readings

* Wikipedia: List (computer science)
* Python for Everyone: Lists

## Multimedia

* YouTube: Python for Informatics - Chapter 8 - Lists
* YouTube: Python Lists
* YouTube: Immutable vs Mutable Objects in Python

## Examples
### Lists

Python supports a number of compound data types used to group other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.
Output:

### Index

List items can be accessed by index.
Output:

### Concatenation

Lists support concatenation.
Output:

### Slicing

Lists may be sliced.
Output:

### Mutable

List items are mutable, i.e. it is possible to change their content.
Output:

### Multidimensional

Multi dimensional lists are lists within lists.
Output:

### Methods

Lists support a variety of methods, including:
* list.append(x) - Adds an item to the end of the list.
* list.insert(i, x) - Inserts an item at a given position.
* list.remove(x) - Removes the first item from the list whose value is x
* list.reverse - Reverse the elements of the list in place.
* list.sort - Sorts the items of the list in place.
* list.clear - Removes all items from the list.
Output:

### Stacks and Queues

The list.pop([i]) method removes the item at the given position in the list, and returns it. If no index is specified, pop removes and returns the last item in the list. This allows for simple stack (last-in, first-out) and queue (first-in, first-out) processing.
Output:

### Split

The string split(<separator>) method returns a list of the words in the string, using separator as the delimiter string. If separator is not specified, runs of consecutive whitespace are regarded as a single separator.
Output:

## Activities
### Tutorials

* Complete one or more of the following tutorials:
** LearnPython
*** Lists
** TutorialsPoint
*** Lists
** Codecademy
*** Lists and Dictionaries
** Wikiversity
*** Python/Lists
** Wikibooks
*** Wikibooks: A Beginner's Python Tutorial/Tuples, Lists, Dictionaries

### Practice

* Create a Python program that asks the user to enter grade scores. Start by asking the user how many scores they would like to enter. Then use a loop to request each score and add it to a list. Finally, display the list of entered scores sorted in descending order and then calculate and display the high, low, and average for the entered scores. Include try and except to handle input errors.
* Create a Python program that asks the user to enter a line of comma-separated grade scores. Use the string split method to parse the line and display the list of entered scores sorted in descending order and then calculate and display the high, low, and average for the entered scores. Include try and except to handle input errors.
* Create a Python program that asks the user to enter a line of text. Use the string split method to parse the line and then display the list of words in alphabetical order with any duplicate words removed from the list.
* Create a Python program that asks the user for a line of text that contains HTML tags, such as: <p><strong>This is a bold paragraph.</strong></p> Use string methods to search for and remove all HTML tags from the text, saving each removed tag in a list. Print the untagged text and then display the list of removed tags sorted in alphabetical order with duplicate tags removed. Include error handling in case an HTML tag isn't entered correctly (an unmatched < or >). Use a user-defined function for the actual string processing, separate from input and output.

## Lesson Summary
### List Concepts

* A list or sequence is a data type that represents an ordered sequence of values, where the same value may occur more than once.
* Lists are a basic example of containers, as they contain other values. If the same value occurs multiple times, each occurrence is considered a distinct item.
* A list can often be constructed by writing the items in sequence, separated by commas, semicolons, or spaces, within a pair of delimiters such as parentheses '', brackets '', braces '', or angle brackets '<>'.
* Some languages may allow list types to be indexed or sliced like array types, in which case the data type is more accurately described as an array.
* An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key.
* Lists can be used to store a list of elements. However, unlike in traditional arrays, lists can expand and shrink, and are stored dynamically in memory.

### Python Lists

* Python lists can be written as a list of comma-separated values (items) between square brackets.
* Lists might contain items of different types, but usually the items all have the same type.
* List items can be accessed by index using the syntax list[index].
* Lists support concatenation using the syntax list + list.
* Lists may be sliced using the syntax list[start:end]. The slice will include the starting index value up to but not including the ending index value.
* List items are mutable, i.e. it is possible to change their content.
* Multidimensional lists are lists within lists, accessed using the syntax list[index][index].
* list.append(x) - adds an item to the end of the list.
* list.insert(i, x) - Inserts an item at a given position.
* list.remove(x) - Removes the first item from the list whose value is x.
* list.reverse - Reverse the elements of the list in place.
* list.sort - Sorts the items of the list in place.
* list.clear - Removes all items from the list.
* list.pop([i]) removes the item at the given position in the list, and returns it. If no index is specified, pop removes and returns the last item in the list.
* string.split(<separator>) returns a list of the words in the string, using separator as the delimiter string. If separator is not specified, runs of consecutive whitespace are regarded as a single separator.

## Key Terms
### aliasing

    A circumstance where two or more variables refer to the same object.

### delimiter

    A character or string used to indicate where a string should be split.

### element

    One of the values in a list (or other sequence); also called items.

### equivalent

    Having the same value.

### index

    An integer value that indicates an element in a list.

### identical

    Being the same object (which implies equivalence).

### list

    A sequence of values.

### list traversal

    The sequential accessing of each element in a list.

### nested list

    A list that is an element of another list.

### object

    Something a variable can refer to. An object has a type and a value.

### queue

    A data structure in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the end of the collection and removal of entities from the start of the collection (first-in, first-out).

### reference

    The association between a variable and its value.

### stack

    A data structure in which the entities in the collection are kept in order and the principal (or only) operations on the collection are push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed (last-in, first-out).

## Review Questions

* A list or sequence is _____. A list or sequence is a data type that represents an ordered sequence of values, where the same value may occur more than once.
* Lists are a basic example of _____. If the same value occurs multiple times, _____. Lists are a basic example of containers, as they contain other values. If the same value occurs multiple times, each occurrence is considered a distinct item.
* A list can often be constructed by _____.  A list can often be constructed by writing the items in sequence, separated by commas, semicolons, or spaces, within a pair of delimiters such as parentheses '', brackets '', braces '', or angle brackets '<>'.
* Some languages may allow list types to be _____ like array types, in which case the data type is more accurately described as an array. Some languages may allow list types to be indexed or sliced like array types, in which case the data type is more accurately described as an array.
* An array is _____. An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key.
* Lists can be used to store a list of elements. However, unlike in traditional arrays, lists can _____, and are stored _____. Lists can be used to store a list of elements. However, unlike in traditional arrays, lists can expand and shrink, and are stored dynamically in memory.
* Python lists can be written as _____. Python lists can be written as a list of comma-separated values (items) between square brackets.
* Lists might contain _____. Lists might contain items of different types, but usually the items all have the same type.
* List items can be accessed by index using the syntax _____. List items can be accessed by index using the syntax list[index].
* Lists support concatenation using the syntax _____. Lists support concatenation using the syntax list + list.
* Lists may be sliced using the syntax _____. Lists may be sliced using the syntax list[start:end]. The slice will include the starting index value up to but not including the ending index value.
* List items are mutable, i.e. _____. List items are mutable, i.e. it is possible to change their content.
* Multidimensional lists are _____. Multidimensional lists are lists within lists, accessed using the syntax list[index][index].
* list.append(x) - _____. list.append(x) - Adds an item to the end of the list.
* list.insert(i, x) - _____. list.insert(i, x) - Inserts an item at a given position.
* list.remove(x) - _____. list.remove(x) - Removes the first item from the list whose value is x.
* list.reverse - _____. list.reverse - Reverse the elements of the list in place.
* list.sort - _____. list.sort - Sorts the items of the list in place.
* list.clear - _____. list.clear - Removes all items from the list.
* list.pop - _____. list.pop - Removes the item at the given position in the list, and returns it. If no index is specified, pop removes and returns the last item in the list.
* string.split - _____. string.split - Returns a list of the words in the string, using separator as the delimiter string. If separator is not specified, runs of consecutive whitespace are regarded as a single separator.

## Assessments

* Flashcards: Quizlet: Python Lists
* Flashcards: Quizlet: Python List Methods
* Flashcards: Quizlet: Python List Operations
* Quiz: Quizlet: Python Lists
* Quiz: Quizlet: Python List Methods
* Quiz: Quizlet: Python List Operations

## See Also

* Python.org: Data Structures
* PythonBasics: lists
* SoloLearn: Python
* Open Book Project: Python 3 - Lists
* This Pointer: Sorting Numerical Arrays in Ascending or Descending Order
* Python Arrays