Ch. 8 Lists & Dictionaries (Python3)

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

List slicing

A programmer can use slice notation to read multiple elements from a list, creating a new list that contains only the desired elements. The programmer indicates the start and end positions of a range of elements to retrieve, as in my_list[0:2]. The 0 is the position of the first element to read, and the 2 indicates last element. Every element between 0 and 2 from my_list will be in the new list. The end position, 2 in this case, is not included in the resulting list. See image:

List Comprehension (1/3)

A programmer commonly wants to modify every element of a list in the same way, such as adding 10 to every element. The Python language provides a convenient construct, known as list comprehension, that iterates over a list, modifies each element, and returns a new list consisting of the modified elements. See image:

Iterating a Dictionary

As usual with containers, a common programming task is to iterate over a dictionary and access or modify the elements of the dictionary. A for loop can be used to iterate over a dict object, the loop variable being set to a key of an entry in each iteration. The ordering in which the keys are iterated over is not necessarily the order in which the elements were inserted into the dict. The Python interpreter creates a hash of each key. A hash is a transformation of the key into a unique value that allows the interpreter to perform very fast lookup. Thus, the ordering is actually determined by the hash value, but such hash values can change depending on the Python version and other factors.

Key argument for sort() and sorted()

Both the list.sort() method and the built-in sorted() function have an optional key argument. The key specifies a function to be applied to each element prior to being compared. Examples of key functions are the string methods str.lower, str.upper, or str.capitalize. See image:

Command Line Arguments

Command-line arguments are values entered by a user when running a program from a command line. A command line exists in some program execution environments, wherein a user can run a program by typing at a command prompt. For example, to run a Python program named "myprog.py" with an argument specifying the location of a file named "myfile1.txt", the user would enter the following at the command prompt: python myprog.py myfile1.txt The contents of this command line are automatically stored in the list sys.argv, which is stored in the standard library sys module. sys.argv consists of one string element for each argument typed on the command line.

Iterating a List (cntd.)

Each iteration of the loop creates a new variable by binding the next element of the list to the name my_var. The loop body statements execute during each iteration and can use the current value of my_var as necessary. Programs commonly iterate through lists to determine some quantity about the list's items. For example, the following program determines the value of the maximum even number in a list: See image:

Dictionaries (cntd.)

In practice, a programmer first creates a dictionary and then adds entries, perhaps by reading user-input or text from a file. Dictionaries are mutable, thus entries can be added, modified, or removed in-place. The table below shows some common dict operations. See image: Dictionaries can contain objects of arbitrary type, even other containers such as lists and nested dictionaries. For example, my_dict['Jason'] = ['B+', 'A-'] creates an entry in my dict whose value is a list containing the grades of the student 'Jason'.

Data structures

Nested dictionaries also serve as a simple but powerful data structure. A data structure is a method of organizing data in a logical and coherent fashion. Actually, container objects like lists and dicts are already a form of a data structure, but nesting such containers provides a programmer with much more flexibility in the way that the data can be organized. Consider the simple example below that implements a gradebook using nested dictionaries to organize students and grades. See image:

List Comprehension (3/3)

Programmers commonly prefer using a list comprehension rather than a for loop in many situations. Such preference is due to less code and due to more-efficient execution by the interpreter. The table below shows various for loops and equivalent list comprehensions. See image:

Common List Slicing Operations

See image:

List slicing (cntd.)

See image:

List methods (cntd.)

See image: The list.sort() and list.reverse() methods rearrange a list element's ordering, performing in-place modification of the list. The list.index() and list.count() return information about the list and do not modify the list.

List nesting

Since a list can contain any type of object as an element, and a list is itself an object, a list can contain another list as an element. Such embedding of a list inside another list is known as list nesting. For example, the code my_list = [[5, 13], [50, 75, 100]] creates a list with two elements that are each another list.

Loops Modifying Lists

Sometimes a program iterates over a list while modifying the elements. Sometimes a program modifies the list while iterating over the list, such as by changing some elements' values, or by moving elements' positions. The below example of changing element's values combines the len() and range() functions to iterate over a list and increment each element of the list by 5. See image:

Copying a List

The problem illustrated by the example above has a simple fix: Iterate over a copy of the list instead of the actual list being modified. Copying the list allows a programmer to modify, swap, add, or delete elements without affecting the loop iterations. See image:

Built-in List Functions supporting List objects

The program above uses the enumerate() built-in function, which results in the variables pos and token being assigned the current loop iteration element's index and value, respectively. Thus, the first iteration of the loop assigns pos to 0, and token to the first user number; the second iteration assigns pos to 1 and token to the second user number, and so on. See image:

in place modification of Lists

Unlike the string sequence type, a list is mutable, and is thus able to grow and shrink without the program having to replace the entire list with an updated copy. Such growing and shrinking capability is called in-place modification. The highlighted lines in the list below indicate ways to perform an in-place modification of the list: See image:

Iterating a List

Use list methods to add or remove elements from the list. The tool accepts only integers or floating point values. A programmer commonly wants to access each element of a list. Thus, learning how to iterate through a list using a loop is critical. Looping through a sequence such as a list is so common that Python supports a construct called a for loop, specifically for iteration purposes. The format of a for loop is shown below: See image:

Use list() to convert view objects into lists

When a program iterates over a view object, one result is generated for each iteration as needed, instead of generating an entire list containing all of the keys or values. Such behavior allows the interpreter to save memory. Since results are generated as needed, view objects do not support indexing. A statement such as my_dict.keys()[0] produces an error. Instead, a valid approach is to use the list() built-in function to convert a view object into a list, and then perform the necessary operations. The example below converts a dictionary view into a list, so that the list can be sorted to find the first two closest planets to Earth. See image:

Index Error

A common error is to try to access a list with an index that is out of the list's index range, e.g., to try to access my_list[8] when my_list's valid indices are 0-7. Accessing an index that is out of range causes the program to automatically abort execution and generate an IndexError.

Dictionary Methods

A dict method is a function provided by the dict type that operates on a specific dict object. Dict methods can perform some useful operations, such as adding or removing elements, obtaining all the keys or values in the dict, merging dicts, etc. See image: Modification of dictionary elements using the above methods is performed in-place. For example, following the evaluation of the statement my_dict.pop('Bob'), any other variables that reference the same object as my_dict will also reflect the removal of 'Bob'. As with lists, a programmer should be careful not to modify dictionaries without realizing that other references to the objects may be affected.

Dictionaries

A dictionary is another type of container object that is different from sequences like strings, tuples, and lists. Dictionaries contain references to objects as key-value pairs - each key in the dictionary is associated with a value, much like each word in an English language dictionary is associated with a definition. Unlike sequences, the elements of a dictionary do not have a relative ordering of positions. The dict type implements a dictionary in Python. See image:

Dictionary Nesting

A dictionary may contain one or more nested dictionaries, in which the dictionary contains another dictionary as a value. Consider the following code: See image: The variable students is first created as an empty dictionary. An indexing operation creates a new entry in students with the key 'John' and the value of another dictionary. Indexing operations can be applied to the nested dictionary by using consecutive sets of brackets []: The expression students['John']['Grade'] first obtains the value of the key 'John' from students, yielding the nested dictionary. The second set of brackets indexes into the nested dictionary, retrieving the value of the key 'Grade'.

Creating Lists

A list can also be created using the built-in list() function. The list() function accepts a single iterable object argument, such as a string, list, or tuple, and returns a new list object. For example, list('abc') creates a new list with the elements ['a', 'b', 'c'].

List Comprehension (2/3)

A list comprehension has three components: 1) An expression component to evaluate for each element in the iterable object. 2) A loop variable component to bind to the current iteration element. 3) An iterable object component to iterate over (list, string, tuple, enumerate, etc). A list comprehension is always surrounded by brackets, which is a helpful reminder that the comprehension builds and returns a new list object. See image:

List methods

A list method can perform a useful operation on a list such as adding or removing elements, sorting, reversing, etc. The table below shows the available list methods. See image:

Indexing Lists

Besides reducing the number of variables a programmer must define, one powerful aspect of a list is that the index is an expression. An index is an integer corresponding to a specific position in the list's sequence of elements. For example, the expression my_list[4] uses an integer to access the element in position 4 of my_list. Replacing the index with an integer variable, such as in my_list[i], allows a programmer to quickly and easily lookup the ith item in a list.

Python Tutor Tool

The below Python Tutor tool executes a Python program and visually shows the objects and variables of a program. The tool shows names of variables on the left, with arrows connecting to bound objects on the right. Note that the tool does not show each number or string character as unique objects to improve clarity.The Python Tutor tool is available at www.pythontutor.com.

Dictionary View Object

The dict type also supports the useful methods items(), keys(), and values() methods, which produce a view object. A view object provides read-only access to dictionary keys and values. A program can iterate over a view object to access one key-value pair, one key, or one value at a time, depending on the method used. A view object reflects any updates made to a dictionary, even if the dictionary is altered after the view object is created. 1) dict.items() - returns a view object that yields (key, value) tuples. 2) dict.keys() - returns a view object that yields dictionary keys. 3) dict.values() - returns a view object that yields dictionary values. See image:

Command Line Arguments (cntd.)

The following program illustrates simple use of command-line arguments, where the program name is myprog, and two additional arguments should be passed to the program. See image: While a program may expect the user to enter certain command-line arguments, there is no guarantee that the user will do so. A common error is to access elements within argv without first checking the length of argv to ensure that the user entered enough arguments, resulting in an IndexError being generated.

Key arguments

The key argument can be assigned any function, not just string methods like str.upper and str.lower. For example, a programmer might want to sort a 2-dimensional list by the max of the rows, which can be accomplished by assigning key to the built-in function max, as in: sorted(x, key=max). See image:

Lists

The list object type is one of the most important and often used types in a Python program. A list is a container, which is an object that groups related objects together. A list is also a sequence; thus, the contained objects maintain a left-to-right positional ordering. Elements of the list can be accessed via indexing operations that specify the position of the desired element in the list. Each element in a list can be a different type such as strings, integers, floats, or even other lists.

enumerate()

The program above uses the enumerate() built-in function, which results in the variables pos and token being assigned the current loop iteration element's index and value, respectively. Thus, the first iteration of the loop assigns pos to 0, and token to the first user number; the second iteration assigns pos to 1 and token to the second user number, and so on. See image:

Indexing Lists (cntd.)

The program can quickly access the Nth oldest person's age using oldest_people[nth_person-1]. Note that the index is nth_person-1 rather than just nth_person because a list's indices start at 0, so the 1st age is at index 0, the 2nd at index 1, etc. A list's index must be an integer type. The index cannot be a floating-point type, even if the value is 0.0, 1.0, etc.

common error when modifying lists with Loops

The program on the right illustrates a common logical error. A common error when modifying a list during iteration is to update the loop variable instead of the list object. The statement num = 0 simply binds the name num to the integer literal value 0. The reference in the list is never changed. In contrast, the program on the left correctly uses an index operation nums[pos] = 0 to modify to 0 the reference held by the list in position pos. The below activities demonstrate further; note that only the second program changes the list's values. See image:

sorted()

The sort() method performs in-place modification of a list. Following execution of the statement my_list.sort(), the contents of my_list are rearranged. The sorted() built-in function provides the same sorting functionality as the list.sort() method, however sorted() creates and returns a new list instead of modifying an existing list.

Creating a Dictionary

There are several approaches to create a dict: 1) The first approach wraps braces { } around key-value pairs of literals and/or variables: {'John': 'A+', 'Brad': 'C-'} creates a dict with two keys 'John' and 'Brad' that are associated with the grades 'A+' and 'C-', respectively. 2) The second approach uses dict comprehension, which evaluates a loop to create a new dict, similar to how list comprehension creates a new list. Dict comprehension is out of scope for this material. 3) Other approaches use the dict() built-in function, using either keyword arguments to specify the key-value pairs or by specifying a list of tuple-pairs. The following creates equivalent dictionaries: - dict(Bobby='805-555-2232', Johnny='951-555-0055') - dict([('Bobby', '805-555-2232'), ('Johnny', '951-555-0055')])

Sorting Lists

sort() One of the most useful list methods is sort(). The sort() method performs an in-place rearranging of the list elements, sorting the elements from lowest to highest. The normal relational equality rules are followed: numbers compare their values, strings compare ASCII/Unicode encoded values, lists compare element-by-element, etc. The sort() method performs element-by-element comparison to determine the final ordering. Numeric type elements like int and float have their values directly compared to determine relative ordering, i.e., 5 is less than 10.


संबंधित स्टडी सेट्स

Psychology Child Development CH.6

View Set

BIOL 1201 Moroney Final Exam: Test 2

View Set

Lesson 6: Chapter 19 Blood Vessels

View Set

Matura MAT OR Prilagojena - Borovo

View Set