• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
[Tut] Python Reverse List with Slicing — An Illustrated Guide

Python Reverse List with Slicing — An Illustrated Guide

Summary: The slice notation `list[::-1]` with default `start` and `stop` indices and negative step size `-1` reverses a given `list`.

Problem: Given a list of elements. How to reverse the order of the elements in the list.

Example: Say, you’ve got the following list:

`['Alice', 'Bob', 'Carl', 'Dora']`

Your goal is to reverse the elements to obtain the following result:

`['Dora', 'Carl', 'Bob', 'Alice']`

## Slicing with Default Start and Stop Values

Slicing is a concept to carve out a substring from a given string.

Use slicing notation `s[start:stop:step]` to access every `step`-th element starting from index `start` (included) and ending in index `stop` (excluded).

All three arguments are optional, so you can skip them to use the default values (`start=0`, `stop=len(lst)`, `step=1`). For example, the expression `s[2:4]` from string `'hello'` carves out the slice `'ll'` and the expression `s[:3:2]` carves out the slice `'hl'`. Note that slicing works the same for lists and strings.

You can use a negative step size (e.g., -1) to slice from the right to the left in inverse order. Here’s how you can use this to reverse a list in Python:

```# Reverse a List with Slicing
names = ['Alice', 'Bob', 'Carl', 'Dora']
names = names[::-1]
print(names)
# ['Dora', 'Carl', 'Bob', 'Alice']```

Python masters know slicing from the inside out. Do you want to improve your slicing skills? Check out my book “Coffee Break Python Slicing” that will make you a slice pro in no time!

## Alternatives Reversing List

Alternatively, you can also use other methods to reverse a list.

• `list.reverse()` — Best if you want to reverse the elements of list in place.
• `list[::-1]` — Best if you want to write concise code to return a new list with reversed elements.
• `reversed(list)` — Best if you want to iterate over all elements of a list in reversed order without changing the original list.

The method `list.reverse()` can be 37% faster than `reversed(list)` because no new object has to be created.

Try it yourself in our interactive Python shell:

Exercise: Run the code. Do all methods result in the same reversed list?

## Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python Reverse List with Slicing — An Illustrated Guide first appeared on Finxter.

https://www.sickgaming.net/blog/2020/10/...ted-guide/

 Possibly Related Threads… Thread Author Replies Views Last Post [Tut] How to Install PIL/Pillow in Python? A Helpful Illustrated Guide xSicKxBot 0 5 10-27-2020, 12:00 AM Last Post: xSicKxBot [Tut] How to Build Your Brand as a Freelance Developer [Ultimate Guide] xSicKxBot 0 6 10-25-2020, 08:58 PM Last Post: xSicKxBot [Tut] How to Read a File Line-By-Line and Store Into a List? xSicKxBot 0 6 10-24-2020, 06:12 PM Last Post: xSicKxBot [Tut] How To Kill A Thread In Python? xSicKxBot 0 8 10-24-2020, 05:40 AM Last Post: xSicKxBot [Tut] Python’s NameError: name ‘xxx’ is not defined — How to Fix This Stupid Bug? xSicKxBot 0 18 10-16-2020, 10:30 PM Last Post: xSicKxBot [Tut] Python IndentationError: unexpected indent (How to Fix This Stupid Bug) xSicKxBot 0 21 10-10-2020, 08:24 PM Last Post: xSicKxBot [Tut] Yield Keyword in Python – A Simple Illustrated Guide xSicKxBot 0 22 10-09-2020, 06:13 PM Last Post: xSicKxBot [Tut] How to Remove Duplicates From a Python List While Preserving Order? xSicKxBot 0 38 10-01-2020, 02:21 AM Last Post: xSicKxBot [Tut] How To Update A Key In A Dictionary In Python If The Key Doesn’t Exist? xSicKxBot 0 43 09-29-2020, 09:02 PM Last Post: xSicKxBot [Tut] How to Get the Last Element of a Python List? xSicKxBot 0 43 09-28-2020, 02:36 AM Last Post: xSicKxBot

Forum Jump:

Become a Patron!
 Upcoming Events

 Latest Threads ODIN 2 Synthesizer VST Last Post: xSicKxBot Yesterday 11:24 PM » Replies: 0 » Views: 2 Microsoft - Cyberattacks target internat... Last Post: xSicKxBot Yesterday 11:24 PM » Replies: 0 » Views: 1 Fedora - What’s new in Fedora 33 Worksta... Last Post: xSicKxBot Yesterday 11:24 PM » Replies: 0 » Views: 1 News - No More Heroes 1 & 2 Are Both Ava... Last Post: xSicKxBot Yesterday 11:24 PM » Replies: 0 » Views: 1 News - Bravely Default 2 Preorders Are L... Last Post: xSicKxBot Yesterday 11:24 PM » Replies: 0 » Views: 2 (Indie Deal) Tropico 6, Assassin's Creed... Last Post: xSicKxBot Yesterday 04:41 PM » Replies: 0 » Views: 4 gRPC performance improvements in .NET 5 Last Post: xSicKxBot Yesterday 04:40 PM » Replies: 0 » Views: 2 News - Season of the Hunt Last Post: xSicKxBot Yesterday 04:40 PM » Replies: 0 » Views: 2 News - Rumour: Hyrule Warriors: Age Of C... Last Post: xSicKxBot Yesterday 04:39 PM » Replies: 0 » Views: 2 News - It’s Been Three Years Since We Fi... Last Post: xSicKxBot Yesterday 04:39 PM » Replies: 0 » Views: 2