In scheme we used append to join two lists together

Info icon This preview shows pages 9–11. Sign up to view the full content.

View Full Document Right Arrow Icon
In Scheme, we used append to join two lists together. In Python, we use append to add one entry to the end of a list. 7.34 Other list mutators To join two lists together, the function is the extend list method. It is a method for the first list, and adds the entries of the second list to the first one. >> L = [1,2, ’abc’ ] >> M = [3,4] >> L . extend ( M ) >> L [1,2,’abc’,3,4] Note: L is not a new list. But if you change the entries of M after executing L . extend ( M ) , then nothing happens to L . 7.35 Yet other list mutators You can use other methods to change lists: the most important is sort . >> L = [1,44,2,90] >> L . sort () >> L [1, 2, 44, 90] Note that sort does not return a value! It mutates the list! If you want a new list whose entries are the same as those in the old list, just sorted, use the function sorted . 7.36 Other ways to combine lists We can also combine lists and make a new one with the addition operator: >> L = [1,2, ’abc’ ] >> M = [3,4,3,4] >> K = L + M >> K [1,2,’abc’,3,4,3,4] There are other list methods as well for you to explore, such as insert , remove , reverse , and others. 7.37 Functions and methods Methods are functions, but their primary argument (the list itself) comes along to the function call when you call the method. In fact, if you call a method of (for example) a list , you’re actually calling a function from the list module, and supplying the first argument of the function. For example, these do the same thing: 9
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
>> B = [4,2,5,1] >> B . sort () and >> B = [4,2,5,1] >> list . sort ( B ) Methods are just a specific kind of function, bound to an object. 7.38 More about mutation There is a distinction between mutable and immutable objects in Python. Numbers aren’t mutable. Neither are strings. (This can be annoying: you can’t change the letters of a string: you have to make a new one.) Lists are mutable. There is a list-like object called a tuple , which we won’t use much, which is like a list except it’s immutable. Most other kinds of complicated data storage are mutable. We’ll see in the next module that this matters because only immutable objects are usable as keys in Python’s dictionary structures. 7.39 4 Functional abstraction in Python Functional abstraction in Python Python lets us write programs with map and filter , much as we did in Module 2 in Scheme. This is handy: quite often we want to find all of the members of a list that satisfy a given property. The statement is: map ( function , list ) filter ( function , list ) This should be familiar: we used ( map f lst ) in Scheme. We used foldr to reduce a list to a single value in Scheme; there is a similar function reduce in Python, but it actually is analogous to foldl , which we didn’t teach you. You can see how it works by looking at reduce . __doc__ . You will not need to use reduce in this course, however. 7.40 Using functional abstraction Here’s an example: # big_enough: int bool def big_enough ( mark ): return ( mark > 50) >> filter ( big_enough , [40, 50, 75, 80, 55]) [75, 80, 55] The function big_enough is of type int bool , and identifies numbers more than 50 .
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern