Write a Python program to reverse All Strings in String List
The program is a python script that reverses the elements in a list of strings.
- A list of strings "val" is defined with 4 strings.
- The original list "val" is printed using the "print()" function.
- The first method of reversing the elements in the list "val" is using a list comprehension. The list comprehension creates a new list "res" with reversed elements. The "i[::-1]" notation is used to reverse each string in the list "val".
- The reversed list "res" is then printed using the "print()" function.
- The second method of reversing the elements in the list "val" is by using the slice notation "[::-1]" on the original list "val". The reversed list is printed using the "print()" function.
Source Code
val = ["Tutor","joes","Computer","Education"]
print ("Original list : ", val)
#First Methods
res = [i[::-1] for i in val]
print ("Reversed list : " , res)
#Second Methods
print("Reversed list : " ,val[::-1])
Output
Original list : ['Tutor', 'joes', 'Computer', 'Education']
Reversed list : ['rotuT', 'seoj', 'retupmoC', 'noitacudE']
Reversed list : ['Education', 'Computer', 'joes', 'Tutor']