Advanced Topics in Python
Lesson 1 of 2
  1. 1
    Let’s start with iterating over a dictionary. Recall that a dictionary is just a collection of keys and values. d = { “Name”: “Guido”, “Age”: 56, “BDFL”: True } print d.items() # => [(‘BDFL’…
  2. 2
    While .items() returns an array of tuples with each tuple consisting of a key/value pair from the dictionary: * The .keys() method returns a list of the dictionary’s keys, and * The .values() me…
  3. 3
    For iterating over lists, tuples, dictionaries, and strings, Python also includes a special keyword: in. You can use in very intuitively, like so: for number in range(5): print number, d = { …
  4. 4
    Let’s say you wanted to build a list of the numbers from 0 to 50 (inclusive). We could do this pretty easily: my_list = range(51) But what if we wanted to generate a list according to some logic…
  5. 5
    Here’s a simple example of list comprehension syntax: new_list = [x for x in range(1, 6)] # => [1, 2, 3, 4, 5] This will create a new_list populated by the numbers one to five. If you want those…
  6. 6
    Great work! Now it’s time for you to create a list comprehension all on your own. c = [‘C’ for x in range(5) if x < 3] print c The example above creates and prints out a list containing [‘C’, ‘C…
  7. 7
    Sometimes we only want part of a Python list. Maybe we only want the first few elements; maybe we only want the last few. Maybe we want every other element! List slicing allows us to access elemen…
  8. 8
    If you don’t pass a particular index to the list slice, Python will pick a default. to_five = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’] print to_five[3:] # prints [‘D’, ‘E’] print to_five[:2] # prints [‘A’, ‘…
  9. 9
    We have seen that a positive stride progresses through the list from left to right. A negative stride progresses through the list from right to left. letters = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’] print l…
  10. 10
    A positive stride length traverses the list from left to right, and a negative one traverses the list from right to left. Further, a stride length of 1 traverses the list “by ones,” a stride lengt…
  11. 11
    Great work! See? This list slicing business is pretty straightforward. Let’s do one more, just to prove you really know your stuff.
  12. 12
    One of the more powerful aspects of Python is that it allows for a style of programming called functional programming, which means that you’re allowed to pass functions around just as if they w…
  13. 13
    Lambda functions are defined using the following syntax: my_list = range(16) filter(lambda x: x % 3 == 0, my_list) Lambdas are useful when you need a quick function to do some work for you. If …
  14. 14
    All right! Time to test out filter() and lambda expressions. cubes = [x ** 3 for x in range(1, 11)] filter(lambda x: x % 3 == 0, cubes) The example above is just a reminder of the syntax.
  15. 15
    First, let’s review iterating over a dict.
  16. 16
    Good! Now let’s take another look at list comprehensions. squares = [x ** 2 for x in range(5)]
  17. 17
    Great! Next up: list slicing. str = “ABCDEFGHIJ” start, end, stride = 1, 6, 2 str[start:end:stride] You can think of a Python string as a list of characters.
  18. 18
    Last but not least, let’s look over some lambdas. my_list = range(16) filter(lambda x: x % 3 == 0, my_list) We’ve given you another (slightly different) garbled. Sort it out with a filter() and …

What you'll create

Portfolio projects that showcase your new skills