Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
shuffle a nested list
#1
Hello can someone explain to me why this code doesnt work out
import random
def randomlist(nums):
    emptylist = []
    for j in range(0, 2**len(nums)):
        emptylist[j].append(random.shuffle(nums))
    print(emptylist)
randomlist([1,2,3])
I want to get a list(in that case is the emptylist) that contains as sublists(the [1,2,3] in that case)shuffled n times
Even tho i use this for loop when it prints the result it prints the list but all the sublists are shuffled in the same way
Reply
#2
The first observation is line 5.
"Append" decides for itself where to put the next item.
You don't need to force the index upon the emptylist.
Paul
Reply
#3
(Nov-12-2020, 04:22 PM)DPaul Wrote: The first observation is line 5.
"Append" decides for itself where to put the next item.
You don't need to force the index upon the emptylist.
Paul

Yes you are right about this.I didnt notice it to be honest but my problem is that even tho i have this for loop every sublist has the same order
Reply
#4
(Nov-12-2020, 04:09 PM)giorgosmarga Wrote: Hello can someone explain to me why this code doesnt work out

What does mean 'doesnt work out'? Don't give expected results?

Python does everything correct:

>>> emptylist = []
>>> emptylist[0].append('whatever')
/.../
IndexError: list index out of range
random.shuffle documentation: "Shuffle list x in place, and return None.". Python obediently returns None-s to emptylist.

And now something totally different: it is maybe also worth to know that functions without return or yield return None.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(Nov-12-2020, 04:42 PM)perfringo Wrote:
(Nov-12-2020, 04:09 PM)giorgosmarga Wrote: Hello can someone explain to me why this code doesnt work out

What does mean 'doesnt work out'? Don't give expected results?

Python does everything correct:

>>> emptylist = []
>>> emptylist[0].append('whatever')
/.../
IndexError: list index out of range
random.shuffle documentation: "Shuffle list x in place, and return None.". Python obediently returns None-s to emptylist.

And now something totally different: it is maybe also worth to know that functions without return or yield return None.
You are right i didnt give much details.I updated my post
Reply
#6
(Nov-12-2020, 04:53 PM)giorgosmarga Wrote: You are right i didnt give much details.I updated my post

Never change original post, explain and clarify in subsequent comments. Otherwise thread doesn't make sense for readers.

I don't get how this code can produce: "every sublist has the same order". There are no sublists, there are None-s:

>>> emptylist = []
>>> emptylist.append(random.shuffle([1, 2, 3]))
>>> emptylist
[None]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
(Nov-12-2020, 05:03 PM)perfringo Wrote:
(Nov-12-2020, 04:53 PM)giorgosmarga Wrote: You are right i didnt give much details.I updated my post

Never change original post, explain and clarify in subsequent comments. Otherwise thread doesn't make sense for readers.

I don't get how this code can produce: "every sublist has the same order". There are no sublists, there are None-s:

>>> emptylist = []
>>> emptylist.append(random.shuffle([1, 2, 3]))
>>> emptylist
[None]
Reply
#8
(Nov-12-2020, 05:09 PM)giorgosmarga Wrote:
(Nov-12-2020, 05:03 PM)perfringo Wrote: Never change original post, explain and clarify in subsequent comments. Otherwise thread doesn't make sense for readers.

I don't get how this code can produce: "every sublist has the same order". There are no sublists, there are None-s:

>>> emptylist = []
>>> emptylist.append(random.shuffle([1, 2, 3]))
>>> emptylist
[None]
def randomlist(nums):
    emptylist = []
    for i in range(2**len(nums)):
        emptylist.append(nums)
    for sublist in emptylist:
        random.shuffle(sublist)
    return emptylist
print(randomlist([1,2,3]))
Because i am confused shouldnt that code return the emptylist with 8 shuffled differenlty sublists?
Reply
#9
(Nov-12-2020, 05:12 PM)giorgosmarga Wrote: Because i am confused shouldnt that code return the emptylist with 8 shuffled differenlty sublists?

No, it shouldn't. There is only one list and you shuffle it 8 times.

Maybe following code can explain:

>>> nums = [1, 2, 3]
>>> emptylist = []
>>> for i in range(3):
...     emptylist.append(nums)     # you are adding same list n times
...
>>> emptylist
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> for item in emptylist:
...     print(id(item))
...
140256603923232                    # same object 3 times
140256603923232                    # or more correctly three references to same object
140256603923232
>>> emptylist[0][0] = 'no way'     # changing item in emptylist
>>> emptylist                      # changes all items because they reference same object 
[['no way', 2, 3], ['no way', 2, 3], ['no way', 2, 3]]   
>>> nums[1] = "it's happening"     # it's getting worse; you change original list
>>> emptylist                      # and as this is same object it is reflected in emptylist
[['no way', "it's happening", 3],
 ['no way', "it's happening", 3],
 ['no way', "it's happening", 3]]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#10
(Nov-12-2020, 05:36 PM)perfringo Wrote:
(Nov-12-2020, 05:12 PM)giorgosmarga Wrote: Because i am confused shouldnt that code return the emptylist with 8 shuffled differenlty sublists?

No, it shouldn't. There is only one list and you shuffle it 8 times.

Maybe following code can explain:

>>> nums = [1, 2, 3]
>>> emptylist = []
>>> for i in range(3):
...     emptylist.append(nums)     # you are adding same list n times
...
>>> emptylist
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> for item in emptylist:
...     print(id(item))
...
140256603923232                    # same object 3 times
140256603923232                    # or more correctly three references to same object
140256603923232
>>> emptylist[0][0] = 'no way'     # changing item in emptylist
>>> emptylist                      # changes all items because they reference same object 
[['no way', 2, 3], ['no way', 2, 3], ['no way', 2, 3]]   
>>> nums[1] = "it's happening"     # it's getting worse; you change original list
>>> emptylist                      # and as this is same object it is reflected in emptylist
[['no way', "it's happening", 3],
 ['no way', "it's happening", 3],
 ['no way', "it's happening", 3]]

Ohh i got it thank you very much.But is there a way to do what i want to do?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 915 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Updating nested dict list keys tbaror 2 1,275 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 4,895 Jan-23-2022, 07:20 PM
Last Post: menator01
  Looping through nested elements and updating the original list Alex_James 3 2,120 Aug-19-2021, 12:05 PM
Last Post: Alex_James
Question Save list with nested list into CSV SpongeB0B 1 5,360 Oct-12-2020, 07:26 AM
Last Post: bowlofred
  Struggling with nested list gr3yali3n 3 2,299 Jul-09-2020, 05:30 PM
Last Post: DPaul
  Nested Dictionary/List tonybrown3 5 3,143 May-08-2020, 01:27 AM
Last Post: tonybrown3
  Help removing asterisk item in a nested list. bmcguire 3 2,588 Apr-06-2020, 02:35 PM
Last Post: snippsat
  Make nested system directories based on an unsorted list? koebi 0 1,595 Mar-25-2020, 01:14 PM
Last Post: koebi
  list approach due nested order 3Pinter 6 2,818 Oct-07-2019, 01:49 PM
Last Post: 3Pinter

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020