-2

I am having trouble with my reverse list program which is below.

list1 = input ("please enter the list you want to print")
print ("Your List: ", list1)
rlist1 = reversed(list1)
toberlist1 = []
for eachit in rlist1:
    toberlist1.append(eachit)
yesno = input ("Press 1 for original list or 2 for reversed list")
yesnoraw = int(yesno)
if yesnoraw == 1:
    print (list1)
else:
    print (toberlist1)

FYI:the toberlist is short for to be reversed list.

The problem I'm having is that the reversed list is returning:

['s', 'r', 'a', 'e', 'p', ',', 's', 'e', 'l', 'p', 'p', 'a']

When it should be returning just:

sraep,selppa

The square brackets, all but one comma and apostrophes are not needed so how do I remove them.

Help would be appreciated!

EDIT:I apologize for posting this question as someone has already answered it but whoever flagged it saying it has already answered is probably someone who knows a thing or two about programming. I didn't know what was wrong which is why I asked the question in the first place. I didn't know that it was a list of strings which meant I couldn't have possibly known to go to that thread. Also the title checker that tells you if there are similar topics also didn't give me any posts that had a similar question. Once again apologies but as a heads up some of us are bad at programming :-)

Derek
  • 301
  • 2
  • 3
  • 7
  • You aren't inputting a list, you are inputting a string representation of a list. That is, `list1` is a single string, not a list containing multiple strings. `input` no longer evaluates the string, like in Python 2. – chepner Aug 20 '15 at 15:03
  • 1
    `reversed` returns a list; if you want a string (which seems likely, despite your variable names), just use extended slicing: `rlist1 = list1[::-1]` – jonrsharpe Aug 20 '15 at 15:04

2 Answers2

1

The square brackets, all but one comma and apostrophes are python's way of telling you that you have asked it to give you a list of strings.

you need to ask it to concatenate this list into a single string:

''.join(list1)
scytale
  • 12,346
  • 3
  • 32
  • 46
  • `list1` is already a single string; this is a no-op that joins the individual characters of that string into a new, identical string. – chepner Aug 20 '15 at 15:04
  • Yeah this worked only that you should have said ' '.join(toberlist1) – Derek Aug 20 '15 at 15:08
1

It seems you have a misunderstanding of what your code is doing at a fundamental level.

list1 = input ("please enter the list you want to print")

Is going to give you a string not a list. So when you do this:

for eachit in rlist1:
    toberlist1.append(eachit)

You are going over each character in the string not each word in the string. If you want the user to enter a comma separated string to get a list of elements you can do:

>>> list1 = input ("please enter the list you want to print").split(',')
please enter the list you want to print1,2,3,4
>>> list1
['1', '2', '3', '4']

split() takes a separator as a parameter and splits your string into chunks on that separator. So entering the comma in there will break the string apart on commas. It doesn't have to be a comma it can be any separator you want, even spaces.

and to reverse it:

>>> list1[::-1]
['4', '3', '2', '1']

This is an example of Python's slice notation.

Community
  • 1
  • 1
kylieCatt
  • 10,672
  • 5
  • 43
  • 51