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)

Why did my list disappear instead of reversing? :(
New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

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?
Posts: 29
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