0

edit: When I asked this I did not properly understand the concept of mutable and immutable objects, and the variables that point to them

I just noticed I wasn't getting a return from random.shuffle(). I realised this makes sense as you would logically want to work with the original list unless specified otherwise.

>>> import string
>>> import random


>>> letters = list(string.ascii_lowercase)

>>> print(letters)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

>>> rand_alpha = random.shuffle(letters)

>>> print(rand_alpha)
None

>>> print(letters)
['f', 'c', 'n', 'u', 'x', 'y', 'q', 'j', 's', 'v', 'w', 'o', 'p', 'z', 't', 'm', 'k', 'd', 'e', 'a', 'g', 'i', 'h', 'l', 'r', 'b']

This led me to wonder whether altering a list in another scope is something people do (and should be cautious not to do accidentally) or is this something special within the Python standard library?

I checked the code - and did a few searches - but I didn't find anything that made this clearer for me.

jshwi
  • 84
  • 1
  • 1
  • 9
  • I have no idea what you're asking. In all cases read the documentation. Some functions work in-place, others don't. – Mad Physicist Jul 01 '20 at 12:30
  • [php's shuffle](https://www.php.net/manual/en/function.shuffle.php) function works simirlay and many other functions in many languages which alter their original arguments – Nikos M. Jul 01 '20 at 12:31
  • `letters[1], letters[0] = letters[:2]` — This is an in-place update in userland code… – deceze Jul 01 '20 at 12:31
  • @deceze Ahh so this is like append with lists and updates with dictionaries then? It just seems inconsistent because random owns the shuffle method whereas the list owns the append, extend etc method – jshwi Jul 01 '20 at 12:34
  • https://stackoverflow.com/a/21222088/476 — Making a copy of the list and returning a new shuffled list would double memory usage… – deceze Jul 01 '20 at 12:36
  • 1
    Whelp, libraries/modules are to save your time. It is not time-expensive to check documentation in comparison to writing functionalities from scratch yourself. – RunTheGauntlet Jul 01 '20 at 12:36

2 Answers2

0

There are functions that work in-place and others that return their result. For the second type, you have to assign their return value to something to make use of it.

Probably the easiest\most common example is the .sort list method and the sorted function; the first works in place much like your shuffle while the second returns a sorted copy of the list passed.

Does this help ?

Ma0
  • 15,057
  • 4
  • 35
  • 65
  • Oh yeah I didn't think of sorted, I just began to be curious about the concept because I've never written a function like that before. – jshwi Jul 01 '20 at 12:37
0

The shuffle fun functions shuffles the list and stores it inside that list only, it does not make a separate list. If you want a separate list, you can duplicate this list and then shuffle it like this

letters=["a","v"]
lettersCopy = letters
lettersCopy.shuffle()

You should always alter dat(lists) carefully because user entered data cannot be retrieved. It is done in other scopes when people want to save storage.

  • `lettersCopy = letters` DOES NOT copy the original list, only re-references it, `lettersCopy = letters[:]` actually copies the original list – Nikos M. Jul 01 '20 at 13:00