Page 120 -
P. 120

10.14. Glossary                                                              97

                             2. Pick an idiom and stick with it.
                                Part of the problem with lists is that there are too many ways to do things. For exam-
                                ple, to remove an element from a list, you can use pop, remove , del, or even a slice
                                assignment.
                                To add an element, you can use the append method or the + operator. Assuming that
                                t is a list and x is a list element, these are right:
                                t.append(x)
                                t = t + [x]
                                And these are wrong:

                                t.append([x])          # WRONG!
                                t = t.append(x)        # WRONG!
                                t + [x]                # WRONG!
                                t = t + x              # WRONG!

                                Try out each of these examples in interactive mode to make sure you understand
                                what they do. Notice that only the last one causes a runtime error; the other three are
                                legal, but they do the wrong thing.
                             3. Make copies to avoid aliasing.
                                If you want to use a method like sort that modifies the argument, but you need to
                                keep the original list as well, you can make a copy.
                                orig = t[:]
                                t.sort()
                                In this example you could also use the built-in function sorted , which returns a new,
                                sorted list and leaves the original alone. But in that case you should avoid using
                                sorted as a variable name!



                           10.14 Glossary

                           list: A sequence of values.
                           element: One of the values in a list (or other sequence), also called items.

                           index: An integer value that indicates an element in a list.
                           nested list: A list that is an element of another list.

                           list traversal: The sequential accessing of each element in a list.
                           mapping: A relationship in which each element of one set corresponds to an element of
                                another set. For example, a list is a mapping from indices to elements.
                           accumulator: A variable used in a loop to add up or accumulate a result.

                           augmented assignment: A statement that updates the value of a variable using an opera-
                                tor like +=.

                           reduce: A processing pattern that traverses a sequence and accumulates the elements into
                                a single result.
   115   116   117   118   119   120   121   122   123   124   125