How to reverse a list in Python

3 Answers

0 votes
lst = ['Python', 'C#', 'Java', 'C', 'PHP', 'C++', 'Java']
 
lst.reverse()
 
print(lst)
 
 
 
'''
run:
 
['Java', 'C++', 'PHP', 'C', 'Java', 'C#', 'Python']
 
'''

 



answered Sep 23, 2017 by avibootz
edited Apr 24, 2024 by avibootz
0 votes
lst = ['Python', 'C#', 'Java', 'C', 'PHP', 'C++', 'Java']
 
lst = lst[::-1]
 
print(lst)
 
 
 
'''
run:
 
['Java', 'C++', 'PHP', 'C', 'Java', 'C#', 'Python']
 
'''

 



answered Sep 23, 2017 by avibootz
edited Apr 24, 2024 by avibootz
0 votes
lst = ['Python', 'C#', 'Java', 'C', 'PHP', 'C++', 'Java']

for val in reversed(lst):
    print(val)

print(lst)    
 
lst = reversed(lst)
 
for s in lst:
    print(s)
 
 
 
'''
run:
 
Java
C++
PHP
C
Java
C#
Python
['Python', 'C#', 'Java', 'C', 'PHP', 'C++', 'Java']
Java
C++
PHP
C
Java
C#
Python

'''
 

 



answered Apr 24, 2024 by avibootz

Related questions

2 answers 126 views
3 answers 247 views
1 answer 203 views
1 answer 219 views
2 answers 167 views
...