Skip to main content
added 21 characters in body; added 1 character in body
Source Link
Elbasel
  • 642
  • 3
  • 11

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

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 

This function should return a string that is comprised from all the letters in the Alphabet except the letters in a pre-defined list. 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 

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 
added 7 characters in body
Source Link
Elbasel
  • 642
  • 3
  • 11

This function should return a string that is comprised from all the letters in the Alphabet except the letters in a pre-defined list. Assuming all letters in the pre-defined list are all lowercase;

I foundcame 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 

This function should return a string that is comprised from all the letters in the Alphabet except the letters in a pre-defined list. Assuming all letters in the pre-defined list are all lowercase;

I found 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 

This function should return a string that is comprised from all the letters in the Alphabet except the letters in a pre-defined list. 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 
Source Link
Elbasel
  • 642
  • 3
  • 11

A function that returns all letters in the Alphabet except the ones in a pre-defined list

This function should return a string that is comprised from all the letters in the Alphabet except the letters in a pre-defined list. Assuming all letters in the pre-defined list are all lowercase;

I found 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