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

Unlock This Lesson

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

Unlock This Lesson

Long Breakfast (Solution)

00:00 Time to cook that breakfast. I’m going to start off by assigning the string that I’ve copied, "eggs, fruit, orange juice" to a variable. breakfast_string

00:15 equals eggs, fruit, orange juice. And now I want to split it to create a list from that. And I can use the split() method to do that.

00:25 Now I’ll have to think about what’s the separator that I want to split on. If I just do the default, it’s going to split on spaces, and that’s going to also split between orange and juice. Right, just for the fun of it.

00:39 breakfast_string.split() And I’m not passing an element in here. You can see that that gives me a list that consists of eggs, fruit, orange, no comma, and then juice.

00:52 And so that’s four elements, which is not how you want to split it. So if you use the .split() method, it’s important that you think about what is the character that you want to split on or characters.

01:03 Because here the best would be to split on comma and space because then I think we should get the three elements that we’re looking for. So I will assign breakfast to

01:18 breakfast_string.split() and then as a separator, I’m going to use comma and space.

01:28 And now we can look at breakfast.

01:32 And that contains three elements: eggs, fruit, and orange juice. That’s proof that these are three elements in the breakfast, the length of breakfast.

01:42 So if I pass breakfast to the len() function, then I get as an output three, which means that there are three elements in that list, which already completes the second task.

01:52 Or I guess we wanted to confirm that it’s three long. So you could do something like saying len() and then passing in breakfast and then saying == 3, and then we get True as a result.

02:07 That’s another way to confirm that it’s actually three elements long. Okay. And then finally, the last task was to get a list comprehension

02:18 of the lengths of each element. All the elements are strings, which means they have a length. And now I want to create a list that I’ll call lengths using a list comprehension.

02:34 And I do that by saying equals, then open up the square brackets, open and close them. And then inside of the square bracket, I’m going to start off by first writing what I want to do to each element, which is calling the len() function on it.

02:49 And I will pass in the breakfast item. So I want to calculate the length of each breakfast item. And I want to do this for breakfast_item in breakfast.

03:07 So breakfast_item is just a loop variable. So you could name this anything you want to as long as you name both of the occurrences the same. And this should give me a list with the lengths of each of the strings.

03:21 "eggs" is four characters long, "fruit" is five characters long, and "orange juice" is 12 characters long and note that also the space counts as a character in here.

03:32 Double check that len() of

03:37 "orange juice" is 12. Okay, that’s it. We could drag it out a little more just to actually have a long breakfast here. What’s another way of getting the length of that list?

03:51 Compare the lengths in the list to calling the len() function directly. Hmm. I could say len(breakfast[2]) == lengths[2]

04:04 and I want that to be equal to True.

04:12 Yay. If you want to draw out your breakfast a little more, you could even write a for loop to make this comparison for each of the elements.

04:20 But I’m full, so I’m moving on.

Become a Member to join the conversation.