Jk128711
Jk128711

Reputation: 13

List Comprehension in a function

So I'm trying to write a function for a hangman game that will return a string that is composed of lowercase English letters - all lowercase English letters that are not in lettersGuessed. I can't see to get the list comprehension to work

def getAvailableLetters(lettersGuessed):
    '''
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters that represents what letters have not
      yet been guessed.
    '''

    [letterGuessed.remove(letter) if letter in'abcdefghijklmnopqrstuvwxyz' for letter in lettersGuessed ]

Upvotes: 0

Views: 272

Answers (3)

ScoJo
ScoJo

Reputation: 139

[letter for letter in 'abcdefghijklmnopqrstuvwxyz' if letter not in lettersGuessed]

To give more of an explanation as to why this works, it helps to consider and list comprehension as the following:

[ expression for item in list if conditional ]

In our case, list is the letters of the alphabet. Our expression is simply the individual letter in the list and our conditional is if the letter does not already exist in lettersGuessed.

The nice thing is that it almost translates into an english sentence which should make it easy to understand.

Give me each letter in the alphabet if the letter is not in the list of guessed letters

I would recommend having a read through this article as having a good understanding of list comprehensions will be a huge benefit for your python coding. https://www.pythonforbeginners.com/basics/list-comprehensions-in-python

Upvotes: 1

mtosch
mtosch

Reputation: 461

For simplicity i'd propose using Python sets

A set is an "unordered collection of unique elements"

def getAvailableLetters(lettersGuessed):
    # convert to set
    all_letters_set = set('abcdefghijklmnopqrstuvwxyz')
    letters_guessed_set = set(lettersGuessed)
    # substract sets
    available_letters = list(all_letters_set - letters_guessed_set)
    # convert list of str to single str
    return ''.join(available_letters)

This way you can do a simple subtraction to retrieve the list of available letters and then join this list to a single string.

No manual iteration needed

Note: if you want to preserve the sorted order of the letters still available, use Pythons sort function before returning the string

available_letters.sort()

Upvotes: 0

Mark Moretto
Mark Moretto

Reputation: 2348

Kind of expanding on @scoJo's response, here's my take.

import string    

test_guesses = ['a','b','f']

def getAvailableLetters(lettersGuessed):
    # Note that we're ensuring that input letters are lowercase when being compared to the list.
    return ''.join([i for i in string.ascii_lowercase if i.lower() not in lettersGuessed])

With the original response, you were removing letters from the letters guessed list, not the alphabet.

My solution also taps into the string standard library of Python to create a list of letters without having to ensure that you've typed each one.

If you want to return a list, just remove the .join() function.

Input:

getAvailableLetters(test_guesses)

Output:

'cdeghijklmnopqrstuvwxyz'

Upvotes: 0

Related Questions