Search code examples
pythonfor-loopnested-loopsenumeratecoding-efficiency

Advice Please - Basic For Loop Efficiency in Python


First off, I want to clarify that I am currently learning Python. So this will probably be a beginner level question. Thanks in advance for any advice/responses.

Currently, the course I'm taking is going into loops and iterations. I decided to do some work on my own and come up with my own challenges for simple practice.

My personal challenge: Create and store a string in a variable, then using enumerator, output the string in reverse order.

**I am aware that there are other ways to accomplish this, however, I'm purposefully trying to learn how much flexibility enumerator can offer.

I went through several formats that I knew where the most efficient, however, the best one I could come up with (based on my current knowledge) was this:

mystring1 = 'Zero'

for index,letter in enumerate(mystring1):
    if index == 0:
        index0 = index,letter
        print('First Index:',index0,'\n')
        index += 1
        if index == 1:
            index1 = index,letter
            print('Second Index:',index1,'\n')
            index += 1
            if index == 2:
                index2 = index,letter
                print('Third Index:',index2,'\n')
                index += 1
                if index == 3:
                    index3 = index,letter
                    print('Fourt Index:',index3,'\n')
                    index += 1
                else:
                    
                    continue
            else:
                
                continue
        else:
            
            continue
    else:
        print('All Letters Captured')
        break
    
    print(index3, index2, index1, index0)

With the above code, this is my output, however, two problems: I'm not grabbing the letter to the corresponding index and I don't feel I accomplished it the most efficiently.

First Index: (0, 'Z') 

Second Index: (1, 'Z') 

Third Index: (2, 'Z') 

Fourt Index: (3, 'Z') 

(3, 'Z') (2, 'Z') (1, 'Z') (0, 'Z')
All Letters Captured

I have a bit of knowledge in C#, and my main fear was to get a an output similar to an array; where I would have all possible combinations.

Any tips or pieces of advice would be awesome. Just trying to learn!

Thanks again for everyone's time!


Solution

  • I think the first problem is that your code is not doing what you want it to do, not that it isn't efficient. Because you have index += 1 at the end of each if statement, the program actually assigns all indexes in the first loop (thats why the letter associated with each index is Z. A cleaner approach that works might look like

    a = []                                                                                                                
    for index,letter in enumerate(mystring1):                                                                                 
        a.insert(0,(index,letter)) 
    

    a is a list, and each time we call a.insert(0,...) we prepend that list (effectively reversing the enumerator). It outputs

    [(3, 'o'), (2, 'r'), (1, 'e'), (0, 'Z')]