0
def listingLyrics(song):
    """
    returns the lyrics of a song in a list, and lowercased
    """

    lyricList = []
    song = song.lower()
    alphabet = 'abcdefghijklmonpqrstuvwxyz' #characters to be used in lyrics
    lyric = ''

    for char in song:
        
        if char not in alphabet:
            lyricList.append(lyric)
            lyric = '' #reset lyric value, but returns many ['']
            continue
        else:
            lyric += char    
    
    lyricList = [e for e in lyricList if e not in ''] #added this to remove [''] from list
    
    return lyricList
        

x = "She! loves you, yeah, yeah, yeah She loves you, yeah, yeah, yeah She loves you, yeah, yeah, yeah, yeah."
y = "can't"

Hey everyone, so I'm doing this as a challenge for myself, a program that returns lyrics of a song in a list, all lowercased. For example, listingLyrics(x) returns ['she', 'loves', 'you', 'yeah', 'yeah', 'yeah', ...]. Is there a more efficient way to make this work, with smaller steps, and perhaps removing the list comprehension? Or is the list comprehension necessary? Can this be done recursively? Thanks.

  • 1
    Does the program work? How about `import re ; re.findall(r"[a-z]+", x.lower())`? I'm not sure why you'd use recursion for this. – ggorlen Apr 13 '21 at 18:20
  • Yeah it works, and I don't know I'm just starting and I learned about Iter vs Recur, so I thought maybe there was a way. And I mean I just tried to make a program using the fundamentals and basics, as a challenge. Thank you, though, that's amazing how simple code can be. Python Tutor shows 4 steps instead of my hundreds – John Johnny Apr 13 '21 at 19:05
  • Yep, it can be simple. It's good to code any solution that works using the tools you know, then optimize later. In general, [recursion isn't useful for processing lists in Python](https://stackoverflow.com/questions/66842109/recursion-applied-to-lists-vs-trees-in-python/66846438#66846438). – ggorlen Apr 13 '21 at 19:08

0 Answers0