Python Programming/Lists

This lesson introduces Python lists.

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

Readings

 * 1)  List (computer science)
 * 2) Python for Everyone: Lists

Multimedia

 * 1) YouTube: Python for Informatics - Chapter 8 - Lists
 * 2) YouTube: Python Lists
 * 3) YouTube: Immutable vs Mutable Objects in Python

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: 1 2 3

Index
List items can be accessed by index.

Output: list[0]: 1 list[1]: 2 list[2]: 3

Concatenation
Lists support concatenation.

Output: list[0]: 1 list[1]: 2 list[2]: 3 list[3]: 4 list[4]: 5

Slicing
Lists may be sliced.

Output: list[0]: 1 list[1]: 2 list[2]: 4 list[3]: 5

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

Output: list[0]: -1 list[1]: -2 list[2]: -3

Multidimensional
Multi dimensional lists are lists within lists.

Output: 1      2       3 4       5       6 7       8       9

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: List: [] After append: [1, 2] After insert: [4, 1, 2, 3] After remove: [1, 2, 3] After reverse: [3, 2, 1] After sort: [1, 2, 3] After clear: []

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: Stack: Last in, first out 3 2 1

Queue: First in, first out 1 2 3

Split
The string split 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: ['This', 'is', 'a', 'test'] ['So', 'is', 'this']

Tutorials

 * 1) Complete one or more of the following tutorials:
 * 2) * LearnPython
 * 3) ** Lists
 * 4) * TutorialsPoint
 * 5) ** Lists
 * 6) * Codecademy
 * 7) ** Lists and Dictionaries
 * 8) * Wikiversity
 * 9) ** Python/Lists
 * 10) * Wikibooks
 * 11) ** A Beginner's Python Tutorial/Tuples, Lists, Dictionaries

Practice

 * 1) 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.
 * 2) 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.
 * 3) 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.
 * 4) Create a Python program that asks the user for a line of text that contains HTML tags, such as:     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 ). Use a user-defined function for the actual string processing, separate from input and output.

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 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).

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