Mateusz Jagiełło
Mateusz Jagiełło

Reputation: 7144

Python - the fastest, most efficient, simplest way to get consonants

I wrote pretty simple code which generate consonants. I wonder is this the best solution?

With best I mean: the fastest, most efficient, simplest way.

import string

letters = string.ascii_lowercase
vowels = 'aeiouy'
consonants = ''.join([letter for letter in letters if letter not in vowels])

Upvotes: 0

Views: 105

Answers (1)

Ben
Ben

Reputation: 6777

If you want the most efficient way, I would just hardcode the values (Python will intern this string literal and won't have to calculate anything each time):

consonants = 'bcdfghjklmnpqrstvwxyz'

It's also the most readable for people reading the code later - it's very obvious what you're after.

Upvotes: 1

Related Questions