Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Combining and Extracting Iterables

00:00 In the previous lesson, I covered built-in functions that process data in your iterables. In this lesson, I’ll look at combining iterables and pulling them apart.

00:09 I’ve got two functions for you. The first, zip(), combines iterables together, while the second, slice(), allows you to get at the parts of an iterable.

00:18 Once more, into the breach. zip() is about combining iterables. It’s easier to show than tell, so let me get some quick data together first.

00:36 Look, I’m back to functions that return an object. That’s nice. It feels comfy. list() trick to the rescue.

00:45 The result here is a list of tuples. Each tuple is a pairing of the items from the two iterables given to zip(). I don’t entirely like the name of the zip() function, as it makes me think compression, but the term comes from the teeth on a zipper, or like a zipper merge on the highway.

01:03 You’re interleaving the two things. The most common use of zip() is to loop through multiple iterables at a time. In a C-fashion loop, you’d use a single counter and access each item by its index.

01:16 You could do that in Python by iterating over range() as a counter, but instead you can zip() the iterables together.

01:30 So zip() stitches them together into a tuple, then each tuple gets unpacked into variables by the for loop, giving you each letter and number from the two lists.

01:45 By the way, zip() can handle more than two iterables. I’ve just used two to keep the example simple. You do need to be careful that the things you’re zipping together are the same size.

01:59 If they aren’t, you get a result that is the size of the smallest of the two iterables. To be a little safer about this, Python 3.10 added the strict argument.

02:15 Using strict=True causes zip() to raise an exception if the two iterables aren’t the same size. You should actually default to using this unless you explicitly want the shortened result.

02:28 Otherwise, if you accidentally pass in two different sized iterables, it may not do what you expect. You’re probably familiar with using square brackets to access parts of a list or to slice it into pieces. Let me get some letters.

02:47 And now if I use square brackets, I get back a subset of the list as a new list, in this case the third, fourth, and fifth letters. The contents in the square brackets are actually turned into an object by the compiler.

03:03 You can create an equivalent object by calling the built-in slice() function. That None there is the step. I’ll come back to it in a minute.

03:12 In the meantime, let me use this object instead of the 2:5.

03:22 Using the slice() object gives me the same result as above. This isn’t one I use very often myself. Even if you’ve got code that needs to calculate the slice point, the regular square bracket notation supports variables, so the function call isn’t very common.

03:38 One use case is if you need to run the same slice on many different collections. I’m not sure if this is any more efficient, but it might make your code a little more readable. Say by having a variable named header slice for the first three lines of an email body instead of always writing 0:3.

03:55 That would also mean you could change the size of the header slice in one location. Let’s go back to that None thing that I skipped over before.

04:06 Slices actually take three arguments, start, stop, and step. Setting the third value here to 2 gives me every other letter within the start-stop range.

04:16 Here, since I’m operating on the whole length, I could leave out the 6 if I wanted.

04:23 And as you’ve probably guessed, to do that with a slice() object, I use None as the argument.

04:32 So the first time round, the None was because the step size was of 1. Passing it in here as the second argument was like having the empty colon set above, meaning slice to the end of the object.

04:47 For more information on using the built-in zip() function, see this video course or tutorial. Next up, how to make things iterable and the associated built-in functions.

Become a Member to join the conversation.