7. Reverse rev[0:0]?


#1

I managed to solve this with the following code:

def reverse(text):
    rev = []
    for r in text:
        rev[0:0] += r        
        
    print "".join(rev)
    return "".join(rev)

reverse('Python!')

Allthough I have no idea how! I was experimenting with the idea of using list position to order the letters, and noticed by accident that eg rev[0:3] reversed half of the word. I wen't down all the way to [0:0] and noticed that whole of the word is reversed.

Does anyone understand why rev[0:0] += r is working?


#2

I also found that rev.insert(0, r) works instead of rev[0:0] += r as it adds the next letter at index 0 every iteration, giving us a reverse of the text.

It seems to me that that the two are connected somehow, but can't see how. Any clues?


#3

well, insert() simply insert on index 0 (prepend).

the other one i will need to have a look. It also seems to prepend/insert at index 0


#4

Please can you explain what [0:0] += r do? Is there something I missed in learning in this course? aaaa :disappointed_relieved:


#5

it inserts r at index 0.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.