What's an in-place change?

This is the place for queries that don't fit in any of the other categories.

What's an in-place change?

Postby Mekire » Fri Feb 08, 2013 4:53 pm

KevinD wrote:Can we think of any more pre-beginner questions to post?

I have been experimenting with lists. I'm trying to reverse my list but it doesn't seem to be working.
Code: Select all
>>> ls = ["a","b","c"]
>>> ls = ls.reverse()
>>> print(ls)
None

Why did my list disappear instead of reversing? :(
User avatar
Mekire
 
Posts: 1143
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: What's an in-place change?

Postby KevinD » Fri Feb 08, 2013 8:32 pm

The "reverse" method changes a list in-place. That is, it changes the list without assigning it to another list. If it were a function, it would return None, so that's why your list is disappearing.

The "sort" method works the same way.

See below:
Code: Select all
>>> ls = ["a","b","c"]

>>> ls.reverse()
>>> ls
['c', 'b', 'a']
>>> ls.sort()
>>> ls
['a', 'b', 'c']
>>>
Quanto lignum posset materiae materietur marmota Chuck si materiam possit materiari foedans, penitus lignum?
KevinD
 
Posts: 30
Joined: Fri Feb 08, 2013 3:15 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 8 guests