16.5. Reversing a ListΒΆ

We can use the looping ability of computers, plus the indexing ability, to manipulate lists in interesting and even surprising ways. In the below program, we use an accumulator, which is something that accumulates values. We start with the accumulator soFar set to an empty list – a list with nothing in it. As the program executes, it appends items from the source list into the soFar list.

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

Note

Notice the [ and ] around the value in the source at the current value of index. This is needed because you can only concatenate two lists together. Try removing the [ and ]. What error do you get?

Note

Discuss topics in this section with classmates.

Show Comments
You have attempted 1 of 3 activities on this page