Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Inplace shuffle function returns none

944 views
Skip to first unread message

Sayth Renshaw

unread,
Oct 18, 2016, 10:49:10 AM10/18/16
to
If shuffle is an "in place" function and returns none how do i obtain the values from it.

from random import shuffle

a = [1,2,3,4,5]
b = shuffle(a)
print(b[:3])

For example here i just want to slice the first 3 numbers which should be shuffled. However you can't slice a noneType object that b becomes.

So how do i get shuffle to give me my numbers?

Cheers

Sayth


Chris Angelico

unread,
Oct 18, 2016, 10:52:34 AM10/18/16
to
In place means that it changes the list. Try print(a) after the shuffle.

ChrisA

John Gordon

unread,
Oct 18, 2016, 10:52:38 AM10/18/16
to
In <e1d9a80e-6c62-49ba...@googlegroups.com> Sayth Renshaw <flebbe...@gmail.com> writes:

> If shuffle is an "in place" function and returns none how do i obtain
> the values from it.

The values are still in the original object -- variable "a" in your example.

> from random import shuffle

> a = [1,2,3,4,5]
> b = shuffle(a)
> print(b[:3])

> For example here i just want to slice the first 3 numbers which should
> be shuffled. However you can't slice a noneType object that b becomes.

> So how do i get shuffle to give me my numbers?

a = [1,2,3,4,5]
shuffle(a)
print(a[:3])

--
John Gordon A is for Amy, who fell down the stairs
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Sayth Renshaw

unread,
Oct 18, 2016, 4:25:19 PM10/18/16
to
So why can't i assign the result slice to a variable b?

It just keeps getting none.

Sayth

bream...@gmail.com

unread,
Oct 18, 2016, 4:54:49 PM10/18/16
to
You are misunderstanding something that is fundamental in Python, namely that anything that is done inplace *ALWAYS* returns None as a warning that the operation has been done inplace, so you're never going to get anything back. All you need do is change your original code as follows:-

from random import shuffle

a = [1,2,3,4,5]
shuffle(a)
b = a[:3]
print(b)

Kindest regards.

Mark Lawrence.

Ian Kelly

unread,
Oct 18, 2016, 5:06:04 PM10/18/16
to
On Tue, Oct 18, 2016 at 2:25 PM, Sayth Renshaw <flebbe...@gmail.com> wrote:
> So why can't i assign the result slice to a variable b?
>
> It just keeps getting none.

Because shuffle returns none. If you want to keep both the original
list and the shuffled list, then do something like:

b = a[:]
shuffle(b)
print(a)
print(b)

Copy it first, then shuffle it.

John Gordon

unread,
Oct 18, 2016, 5:36:08 PM10/18/16
to
In <9d24f23c-b578-4029...@googlegroups.com> Sayth Renshaw <flebbe...@gmail.com> writes:

> So why can't i assign the result slice to a variable b?

Because shuffle() modifies the list directly, and returns None.
It does NOT return the shuffled list.

Steve D'Aprano

unread,
Oct 18, 2016, 8:44:45 PM10/18/16
to
On Wed, 19 Oct 2016 07:25 am, Sayth Renshaw wrote:

> So why can't i assign the result slice to a variable b?
>
> It just keeps getting none.

Of course you can assign the result slice to b. You just have to do it the
right way.

You keep getting None because you do it the wrong way. Unfortunately you
aren't showing us your code, so we have no idea what you are doing wrong.
My guess is that you are doing something like this:


a = [1, 2, 3, 4, 5, 6, 7, 8]
b = random.shuffle(a)[0:3]

That's wrong -- shuffle() modifies the list you pass, and returns None. You
cannot take a slice of None. Try this:

a = [1, 2, 3, 4, 5, 6, 7, 8]
random.shuffle(a)
b = a[0:3]
print(b)




--
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

Sayth Renshaw

unread,
Oct 19, 2016, 3:02:07 AM10/19/16
to
Ok i think i do understand it. I searched the python document for in-place functions but couldn't find a specific reference.

Is there a particular part in docs or blog that covers it? Or is it fundamental to all so not explicitly treated in one particular page?

Thanks

Sayth

Peter Otten

unread,
Oct 19, 2016, 4:01:26 AM10/19/16
to
Steve D'Aprano wrote:

> On Wed, 19 Oct 2016 07:25 am, Sayth Renshaw wrote:
>
>> So why can't i assign the result slice to a variable b?
>>
>> It just keeps getting none.
>
> Of course you can assign the result slice to b. You just have to do it the
> right way.
>
> You keep getting None because you do it the wrong way. Unfortunately you
> aren't showing us your code, so we have no idea what you are doing wrong.
> My guess is that you are doing something like this:
>
>
> a = [1, 2, 3, 4, 5, 6, 7, 8]
> b = random.shuffle(a)[0:3]
>
> That's wrong -- shuffle() modifies the list you pass, and returns None.
> You cannot take a slice of None. Try this:
>
> a = [1, 2, 3, 4, 5, 6, 7, 8]
> random.shuffle(a)
> b = a[0:3]
> print(b)

But once you understand how it works consider

>>> a = [1, 2, 3, 4, 5, 6, 7, 8]
>>> random.sample(a, 3)
[1, 5, 2]

instead. This should be more efficient for "small" samples and leaves `a`
intact:

>>> a

Ben Finney

unread,
Oct 19, 2016, 4:02:45 AM10/19/16
to
Sayth Renshaw <flebbe...@gmail.com> writes:

> Ok i think i do understand it. I searched the python document for
> in-place functions but couldn't find a specific reference.

They aren't a separate kind of function.

A function can do anything Python code can do; indeed, most Python
programs do just about everything they do inside functions.

A function also, if it returns, returns a value. (Sometimes that is the
value ‘None’.)

There's no special distinction between functions that return ‘None’
versus those that don't. There's no special distinction between
functions that have other effects versus those that don't.

> Is there a particular part in docs or blog that covers it? Or is it
> fundamental to all so not explicitly treated in one particular page?

I don't really understand the question, but I hope that addresses it.

--
\ “Prayer must never be answered: if it is, it ceases to be |
`\ prayer and becomes correspondence.” —Oscar Wilde, _The Epigrams |
_o__) of Oscar Wilde_, 1952 |
Ben Finney

Chris Angelico

unread,
Oct 19, 2016, 4:25:25 AM10/19/16
to
On Wed, Oct 19, 2016 at 6:01 PM, Sayth Renshaw <flebbe...@gmail.com> wrote:
> Ok i think i do understand it. I searched the python document for in-place functions but couldn't find a specific reference.
>
> Is there a particular part in docs or blog that covers it? Or is it fundamental to all so not explicitly treated in one particular page?

It's not a rule, it's a design principle. So the best way to find out
about it is either to look at hundreds or thousands of Python standard
library functions and recognize a pattern... or ask on a mailing list
and be told :)

ChrisA

Sayth Renshaw

unread,
Oct 19, 2016, 3:19:08 PM10/19/16
to
Hi Chris

I read this last night and thought i may have woken with a frightfully witty response.

I didnt however.

Thanks :-)
0 new messages