5.3 Create list comprehensions - Video Tutorials & Practice Problems
Video duration:
8m
Play a video:
Video transcript
<v ->Okay, and let's look briefly</v> at creating list comprehensions. So this is just a fancy Python way to create lists and other data structures in a very small amount of space. So usually just one line, and we're gonna look at it because it can be very helpful for reducing the amount of code in our word guessing game. So let's open our scratch.py first, and look at creating a basic list. So, if you remember, we created a word earlier display_ word = the empty string, and then said for letter in our word hello, add, an underscore and a space to the display word, and then print that word. So this was an earlier version, and we are looping through something and then creating a new thing based off of it. And then getting just that final thing. So this is like kind of like format that lists comprehensions are useful for. Creating something from something else. So, in this case, I've got this, that's cool, but I can re-write this in one line by saying display_word. So, actually it creates, it would be two lines 'cause I'm going to create a list first, and then I'm gonna convert that list into just a single string. So display_word is equal to an underscore and a space for letter in the word hello, and let's run this. And it gives me a new list that has each of those letters, and then I can combine that into a single word by doing this, the empty string, and then join display_word. So this is a string method that says take a list and join them together into a string using this as a delimiter, or putting this in-between everything. So, if I used a comma there, then there would be commas in-between each of these things. We can also re-write this. Instead of having the space here, we can put the space here and now we have five underscores with spaces in-between, and we now we don't even have that space at the very end, which is great. We can create other kinds of lists comprehensions so like squares = n**2 for n in range 10 and display squares. Let's see what that comes out. So our range goes from zero to nine, and then it will create this for loop, and then take each of those n values and square them to create the new list. So yeah, if I did n+1 it wouldn't be squares anymore, but it would be like just adding one to this value n. So that's how you can create a basic list comprehension. We can actually extend this and include an if statement in there because our code used an if statement inside of the for loop. So, in this word_guess file, I've got this function inside of the for loop, and there should be a guessed_letters here, guessed_letters = let's say e and o and a. So I'll run this again. Make sure that it looks right, and then let's work on translating this into a list comprehension. So display_word_ list because it's gonna create a list first, and then we're gonna join them, is going to be, so at first it's gonna be the letter if it's in the guessed_letters or it's gonna be this. So I'm gonna say letter if letter in guessed_ letters else the underscore for letter in oh, hello. So this is a little crazy. It's a super long line. I can also say put it on two lines if I wanted to, and then I'll join them, join the list, using spaces in-between, and I end up with the same result. So I can also print display_word_list, so you can see that it's actually creating a list and it is putting an underscore in, if in the else case, and putting the letter in if letter is in guessed_letters. I can change what letters are in here. Now it's h and e. So that works. It seems a little magical. It takes a while to get used to. It's useful for you to know that it's a thing you that you can do and that exists. So we can now use this to refactor one of the functions in our word guess solution. So I'm gonna look at this refactored one, and I've already extracted the function to get_display_word, and I can replace it with this, so letter_list and then return, the space, join letter_list. And this should be answer.