Python Why does this code work, python

AI Thread Summary
The discussion revolves around understanding a Python function designed to reverse a list. The key point of confusion is the third line of the function, which swaps elements in the list. This line is crucial because it simultaneously exchanges two elements, preventing data loss that would occur if only one element were reassigned. The method used is unique to Python, allowing for a concise swap without the need for a temporary variable, unlike traditional methods in other programming languages. Additionally, the conversation touches on the use of negative indexing in Python, which simplifies accessing elements from the end of the list. While the provided code serves as a good academic exercise, it is noted that the most efficient way to reverse a list in Python is by using the built-in 'reverse' method, which is faster than both the tuple swapping method and the temporary variable approach. The underlying purpose of the exercise is to illustrate how similar operations might be handled in C, providing insight into programming concepts across different languages.
mr.me
Messages
49
Reaction score
0
This isn't quite homework, it was just in my textbook as an illustration. Below is a function to return a list in reverse order.

Why does it work? My brother tried to explain it to me but failed to clarify. The only line I don't understand is the third and what process takes place because of it.

Code:
def list_reverse(a_list):            
     for index in range(len(a_list) / 2):       
         a_list[index],a_list[-index-1] = a_list[-index-1],a_list[index]
    
     return a_list

this_list=["happy", 1,2,3,4,5]
print list_reverse(this_list)
 
Technology news on Phys.org
basically, in a list there are n elements, with indices 0, 1, 2, 3, ... , n-1

in the third line, you assign element n-1 to index 0, assign element n-2 to index 2, assign element n-1-x to index x, as well as the reverse, ie assign element x to index n-1-x

you have to do it in pairs because if you simply assign element x into index n-1-x, what was originally as element n-1-x would then be lost and (in most cases) irrecoverable
 
That third line is very tricky. The purpose of that line is to swap two elements of a list. Although you can do this in pretty much any programming language, the way it is done here is unique to Python.

The more usual way of swapping two elements in a list is this:
1) Store the value of the first variable in a temp variable.
2) Store the value of the second element in the first variable.
3) Store the value of temp in the second variable.

Here is Python code for reversing the elements of a list, using this technique.
Code:
def list_reverse(a_list):            
	for index in range(len(a_list) / 2):       
		temp = a_list[index]
		a_list[index] = a_list[-index-1]
		a_list[-index-1] = temp
		    
	return a_list

this_list=[0, 1,2,3,4,5, 6, 7, 8, 9]
print this_list
print list_reverse(this_list)

The other thing that is tricky about the code you showed is its use of negative indexes for the list. My code example can be rewritten so that it doesn't use negative list indexes.
Code:
def list_reverse(a_list):
	length = len(a_list)            
	for index in range(len(a_list) / 2):       
		temp = a_list[index]
		a_list[index] = a_list[len -index-1]
		a_list[len -index-1] = temp
		    
	return a_list

this_list=[0, 1,2,3,4,5, 6, 7, 8, 9]
print this_list
print list_reverse(this_list)
 
This a good academic exercise, but everyone knows the real way to reverse a list in Python is to use the 'reverse' method of the list class, right? It's going to be faster than the Python code using tuples, and even faster than the pseudo-C code using 'temp'. Just checking...
 
Dick Said
Re: Why does this code work, python
This a good academic exercise, but everyone knows the real way to reverse a list in Python is to use the 'reverse' method of the list class, right? It's going to be faster than the Python code using tuples, and even faster than the pseudo-C code using 'temp'. Just checking...


Yes, that's understood

I think the point was to give us Hint as to What C might behave like in this instance
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
3
Views
3K
Replies
5
Views
2K
Replies
3
Views
1K
Replies
2
Views
1K
Replies
11
Views
1K
Back
Top