Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python good programming practice for enumerating lists

Tags:

python

list

I'm pretty new to Python and programming in general, and I was wondering if it is a good programming practice to write long statements with many logic operators - for example, in a for loop.

For example, here's a function I made that gets all the vowels from a word and returns a list containing those vowels.

def getVowels(word):
    vowel_list = []
    index = 0
    for i in word:
        if i == "a" or i == "e" or i == "i" or i == "o" or i == "u" or i == "A" or i == "E" or i == "I" or i == "O" or i == "U":
            vowel_list.append(word[index])
        index += 1
    return vowel_list

As you can see, the if statement has gotten very long. Is it considered good programming? If it's not, is there a better way to code this function?

like image 914
stensootla Avatar asked Apr 07 '12 13:04

stensootla


People also ask

Can you enumerate a list in Python?

Instead of using the range() function, we can instead use the built-in enumerate() function in python. enumerate() allows us to iterate through a sequence but it keeps track of both the index and the element. The enumerate() function takes in an iterable as an argument, such as a list, string, tuple, or dictionary.

Is enumerate faster than for loop?

Using enumerate() is more beautiful but not faster for looping over a collection and indices.

Does Python enumerate start at 0?

As in the example above, by default, the index of enumerate() starts at 0. If you want to start from another number, pass the number to the second argument of enumerate() .

What happens when you enumerate a list in Python?

The enumerate function in Python converts a data collection object into an enumerate object. Enumerate returns an object that contains a counter as a key for each value within an object, making items within the collection easier to access.


2 Answers

No it is not considered good practice, there are always better ways :D

if i.upper() in "AEIOU"

Here is a much shorter version of your function using list comprehensions:

def get_vowels(word):
    vowels = "AEIOU"
    return [c for c in word if c.upper() in vowels]
like image 133
jamylak Avatar answered Oct 15 '22 16:10

jamylak


Would probably be better to use sets:

VOWELS = set('aeiouAUIOU')

def get_vowels(word):
    return [c for c in word if c in VOWELS]

or, more geeky:

def get_vowels(word):
    return filter(VOWELS.__contains__, word)

(But the first approach is most readable and as such is more pythonic. Also, the second function will return generator, not a list in Python 3.)

EDIT performance comparison of c in list vs c in set:

import timeit

VOWELS = 'aeiouAEIOU'
VOWSET = set(VOWELS)
SAMPLE = 'asflasrjoperugASDFAROUAoarfpeoriugargagadropgue'

def get_vowels(word, vowels):
    return [c for c in word if c in vowels]

print timeit.timeit('get_vowels(SAMPLE, VOWELS)', 
                    'from __main__ import VOWELS, SAMPLE, get_vowels') 
# ^ prints 10.0739870071
print timeit.timeit('get_vowels(SAMPLE, VOWSET)', 
                    'from __main__ import VOWSET, SAMPLE, get_vowels') 
# ^ prints 9.43965697289
like image 45
bereal Avatar answered Oct 15 '22 15:10

bereal