This function should return a string that is comprised from all the letters in the Alphabet except the letters in a pre-defined list in alphabetical order.
Assuming all letters in the pre-defined list are all lowercase;
I came up with two ways to do this and I would like to know which way is more efficient or more 'Pythonic'. Thanks in advance.
The first way:
import string
def get_remaining_letters(list_of_characters):
# casting the Alphabet to a list so that it becomes mutable
list_of_all_letters = list(string.ascii_lowercase)
# looping for each character in the pre-defined list
for char in list_of_characters :
if char in list_of_all_letters: # to avoid possible errors caused by the .remove in case of a duplicate charcter in the pre-defined list
list_of_all_letters.remove(char)
return ''.join(list_of_all_letters) # returns a string
# ----------------------------------------------------------- #
pre_defined_list = ['a', 'b', 'b']
test = get_remaining_letters(pre_defined_list)
print(test)
output :
>>> cdefghijklmnopqrstuvwxyz
The second way:
import string
def get_remaining_letters(list_of_characters):
if list_of_characters == []:
return string.ascii_lowercase
else:
remaining_letters = ''
for char in string.ascii_lowercase:
if char not in list_of_characters:
remaining_letters += char
return remaining_letters
pre_defined_list = ['a', 'b', 'b']
test = get_remaining_letters(pre_defined_list)
print(test)
output :
>>> cdefghijklmnopqrstuvwxyz