Working with Lists in Python - Tutorial Image

Working with Lists in Python - Tutorial

This tutorial was built in Python 3.6

A Simple List

my_list = [1, 2, 3, 4] print(my_list)

Reversing a List

This quick and easy technique shows how you can access all the elements of a list in reverse order.

1
2
3
>>> my_list = [1,2,3,4,5]
>>> my_list[::-1]
[5, 4, 3, 2, 1]

Try it Out

my_list = [1, 2, 3, 4] print(my_list[::-1])

We can then treat our reversed list as an iterator and iterate through every element within this reversed list like so:

1
2
3
4
5
6
7
8
>>> for el in my_list[::-1]:
...     print(el)
...
5
4
3
2
1

Conclusion

If you found this tutorial useful or require further assistance then please let me know in the comments section below or by tweeting me: @Elliot_F.

Was This Post Helpful?
Submit a PR: Edit on Github