Why does this code work, python

  • Context: Python 
  • Thread starter Thread starter mr.me
  • Start date Start date
  • Tags Tags
    Code Python Work
Click For Summary

Discussion Overview

The discussion revolves around understanding a Python function that reverses a list. Participants explore the mechanics of the code, particularly focusing on the swapping of elements and the use of negative indexing. The conversation also touches on alternative methods for reversing lists in Python.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant seeks clarification on how the third line of the function works, particularly the swapping mechanism.
  • Another participant explains that the third line swaps two elements in the list and discusses the importance of doing this in pairs to avoid losing data.
  • A different participant contrasts the provided code with a more traditional method of swapping elements using a temporary variable, noting that the approach is unique to Python.
  • Some participants mention the use of negative indexing as a tricky aspect of the original code.
  • One participant suggests that using the built-in 'reverse' method of the list class is a more efficient way to reverse a list in Python.
  • A later reply acknowledges the academic nature of the exercise and suggests it serves as a hint towards understanding similar behavior in C programming.

Areas of Agreement / Disagreement

Participants express differing views on the best method to reverse a list in Python, with some advocating for the original code and others favoring the built-in 'reverse' method. The discussion remains unresolved regarding which method is superior.

Contextual Notes

There are references to alternative methods and comparisons to other programming languages, but no consensus is reached on the best approach to list reversal in Python.

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
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
55
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 11 ·
Replies
11
Views
1K