Search code examples
pythonloopsindexingmusic-notation

python looping index


I am trying to write a program for getting the notes of a musical scale. This is what I've got up to now but it seems ridiculously complicated!! what am I missing? Is it supposed to be like that?

notes = [ "c", "c#", "d", "d#", "e", "f", "f#", "g", "g#", "a", "a#", "b" ]
major = [2,2,1,2,2,2] # semitone steps
root  = "f"
root_i= notes.index(root)

index = [root_i+i for i in [sum(major[:y]) for y in range(len(major)+1)]]
scale = [notes[i] if i < len(notes) else notes[i-len(notes)] for i in index]

I just need to increment the root_i by each "step" in major and restart when i reach the end of notes...

Thanks.


Solution

  • The simplest?

    scale = [notes[(y+root_i)%len(notes)] for y in [0,2,4,5,7,9,11]]
    

    or even

    scale = [notes[(y+notes.index(root))%len(notes)] for y in [0,2,4,5,7,9,11]]
    

    you don't need root_i or index