0

Below is a bit of code which seems to work at the start but populates my holding variable with half while leaving the other have in the list that I'm trying to give list elements from. The last element specifically. I don't know why this is not iterating through the entire flist. Help?

Thanks Sembor

def reverse(text):
    flist = []
    holding = ""
    for i in str(text):
      flist.append(i)
    print(flist)

    for i in flist:
      holding = holding + flist[-1]
      del flist[-1]
      print(holding)


    print(flist)    
reverse("JamesBond")
idjaw
  • 25,487
  • 7
  • 64
  • 83
Sembor
  • 1
  • 2
  • 3
    Never modify the list while iterating over the same list. You will end up with unexpected and unwanted behaviour like you are most likely experiencing now. – idjaw Jul 22 '17 at 04:36

5 Answers5

1

Instead of trying to modify your list while iterating over it. Instead, what you should do, is simply create a new string, and append the letters in the reverse order. You can do this by counting backwards using range:

def reverse(text):
    reverse_string = ""
    for i in range(len(text) - 1, -1, -1):
        reverse_string += text[i]
    print(reverse_string)
reverse("JamesBond")

However, the best way to do this in python, is by making use of slicing, in which, you can simply do:

reverse_string = text[::-1]
idjaw
  • 25,487
  • 7
  • 64
  • 83
1

As this comment says, the problem is that you're modifying the list as you're iterating over it.

Basically, for i in range plist isn't going through all the elements since you making the list shorter as you loop through it. This is what's happening to your list as you're iterating it.

holding    plist
d          ["J","a","m","e","s","B","o","n","d"]
             ^
dn         ["J","a","m","e","s","B","o","n"]
                 ^  
dno        ["J","a","m","e","s","B","o"]
                     ^
dnoB       ["J","a","m","e","s","B"]
                         ^
dnoBs      ["J","a","m","e","s"]
                             ^ #can't loop any further  

What you can do instead is something like this

for i in range(len(plist), -1, -1):
    holding = holding + plist[i]

Or this

def reverse(text):
    return text[::-1]

Or you could use the built in reversed function, like equaio did.

Farai Gandiya
  • 79
  • 1
  • 4
0

When creating lists from strings you can use list.comprehension:

[i for i in text]

And you could reverse this by using enumerate for instance:

[text[-ind-1] for ind, i in enumerate(text)]

Anton vBR
  • 18,287
  • 5
  • 40
  • 46
  • This gives a list instead of a reversed string. Also no need for enumeration: `"".join([s[-i-1] for i in range(len(s))])`. – agtoever Jul 22 '17 at 06:02
0

as I understand you want to take the string "JamesBond" and reverse it to "dnoBsemaJ". I am also extremely new to Python. I gave your function a try and came up with the following. It may not be the prettiest but it does reverse the text.

def reverse(_string):
    _stringLen = len(_string)
    i = _stringLen - 1
    _newstring = ""
    while i >= 0:
        _newstring = _newstring + _string[i]
        i -= 1
    return _newstring


print reverse("JamesBond")

Prints out : dnoBsemaJ

Paul M.
  • 23
  • 4
0

Splicing can help you unless you do want to write the algorithm to reverse

>>> k = 'JamesBond'
>>> rev_str = k[::-1]
>>> rev_str
'dnoBsemaJ'
>>> type(rev_str)
<type 'str'>
Ajay2588
  • 527
  • 3
  • 6