0

I have to print letters from an array backwards. I got all the letters to be backwards but I realized I used the sort method and I'm not allowed to use it. I can't figure out any other way. Any suggestions?

The output should be:

w

v

u

t

.
.
.

g

f

This is the code I have so far:

letter = ['f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w']
letter.sort(reverse=True)





for i in range(len(letter)):
print(letter[i])
4

7 Answers 7

1
letters = 'fghijklmnopqrstuvw'

for letter in reversed(letters):
    print(letter)
  • Strings work like lists. A string is a list of characters.
  • reversed() can be used to reverse the order of a list.
  • There is no need to use range()
Sign up to request clarification or add additional context in comments.

Comments

1

you can use use the built-in function reversed :

print(*reversed(letter), sep='\n')

output:

w
v
u
t
s
r
q
p
o
n
m
l
k
j
i
h
g
f
  • *reversed(letter) will give as non-keyword arguments all the letters in reverse order for the print built-in function
  • the keyword argument sep='\n' will ensure that all the letters will be printed on a separate line

Comments

0
letter = ['f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w']
letter[::-1]

OR

reverseletter=letter[::-1]

Comments

0

To reverse a list you can use.

  1. Slicing [::-1]
for i in letters[::-1]:
    print(i)
  1. You can use reversed.
for i in reversed(letter):
    print(i)

Note: reversed spits an iterator.

Comments

0

you can use revered() method to print it in reverse order such as below

letter = ['f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w']

for i in reversed(letter): 
    print(i)

Comments

0

letterrev=letter[::-1]

for i in letterrev: print(i)

use this one

Comments

0

You can directly use list indexing or slicing such as:

letter = ['f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w']

print(letter[::-1])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.