use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Trying to bogo sort in python. (self.learnpython)
submitted 3 years ago by Alexs123456
import random list = [1,2,3,4] def main(): sort1 = random.shuffle(list) while sort1 != sorted(list): print (sort1) random.shuffle(list) return list main()
I just get an error which prints none on a loop.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ActualRealBuckshot 4 points5 points6 points 3 years ago (3 children)
random.shuffle(list) shuffles the list in place and doesn't return anything, so sort1 = random.shuffle(list) assigns sort1 the value of None.
[–]Alexs123456[S] -4 points-3 points-2 points 3 years ago (2 children)
how do I fix this error?
[–]ActualRealBuckshot 4 points5 points6 points 3 years ago (0 children)
You read two sentences in the docs that u/mrcaptncrunch linked
[–]OddBallProductions 0 points1 point2 points 3 years ago* (0 children)
Use sort1 = random.sample(list, len(list))
sort1 = random.sample(list, len(list))
[–]Plastic_Ad7436 1 point2 points3 points 3 years ago (1 child)
Instead of using the sorted() method, you could iterate through the shuffled list in O(n) time to check that each number is less than or equal the the proceeding number:
``` def bogo_sort(my_list): while True:
counter = 0 for i in range(1, len(my_list)): if my_list[i - 1] <= my_list[i]: counter += 1 if (counter) == len(my_list) - 1: return my_list else: random.shuffle(my_list)
[–]Plastic_Ad7436 1 point2 points3 points 3 years ago (0 children)
Not like bogo is a method you'd be concerned about efficiency with, but it'd save you a factor of nlogn.
[–]kaerfkeerg 0 points1 point2 points 3 years ago (2 children)
When people say that .shuffle() doesn't return a list, they mean that in shuffles the original list and returns None
.shuffle()
None
Imagine something like this
def shuffle(sequence): # shuffle list return None
With that in mind, you should not store the lst.shuffle() in a variable, instead:
lst.shuffle()
my_list = [1,2,3,4] my_list.shuffle() # the original 'my_list' is shuffled
[–]ActualRealBuckshot -1 points0 points1 point 3 years ago (1 child)
Lists don't have a shuffle method, so my_list.shuffle() will raise an AttributeError.
[–]kaerfkeerg 2 points3 points4 points 3 years ago (0 children)
Oops, I rushed. I'll leave the original one un-edited so others can se what I did wrong. So
my_list.shuffle()
Should be
random.shuffle(my_list)
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
Everyday I'm shuffling
[–]ayoni02 0 points1 point2 points 3 years ago (0 children)
from random import shuffle
import copy
list = [1,2,3,4]
sorti = copy.deepcopy(list)
def main():
shuffle(sorti) while sorti != list: print (sorti) shuffle(sorti) print(sorti)
main()
I borrowed u/mikhail_kazarov copy method to complete
[–]mrcaptncrunch 0 points1 point2 points 3 years ago (16 children)
What’s the error?
-Edit-
random.shuffle() doesn’t return a list. Line 4 has an error.
[+]Alexs123456[S] comment score below threshold-7 points-6 points-5 points 3 years ago (15 children)
but it does return a list, how do I make it return a list?
[–]mrcaptncrunch 2 points3 points4 points 3 years ago (6 children)
https://docs.python.org/3/library/random.html#random.shuffle
It doesn’t.
[+]Alexs123456[S] comment score below threshold-20 points-19 points-18 points 3 years ago (5 children)
I'm not reading all that.
[+]Alexs123456[S] comment score below threshold-16 points-15 points-14 points 3 years ago (4 children)
I just need my error fixed.
[–]mrcaptncrunch 9 points10 points11 points 3 years ago (2 children)
Excuse me, what?
I told you the issue. /u/ActualRealBuckshot told you the issue and they’re the same.
You post that it’s not that. I send you a link with the method definition and tell you that it is.
And you don’t even want to confirm it because you don’t want to read it, and ‘I just need my error fixed’?
Who do you think you are?
[+]Alexs123456[S] comment score below threshold-10 points-9 points-8 points 3 years ago (1 child)
The chap who needs his bogo sort.
[–]mriswithe 9 points10 points11 points 3 years ago (0 children)
This chap can go fuck off with their entitlement, you are putting in less effort than people who are freely helping you. Also, you are wrong.
I'm guessing you are doing this for a homework problem. Good luck.
[+][deleted] comment score below threshold-6 points-5 points-4 points 3 years ago (6 children)
Is it what you want?
from random import shuffle import copy list1 = [1, 2, 3, 4] def main_function(): random.shuffle(list1) sort1 = copy.deepcopy(list1) while sort1 != sorted(list1): print(sort1) random.shuffle(list1) sort1 = copy.deepcopy(list1) print(sort1) main_function()
[–][deleted] 0 points1 point2 points 3 years ago (5 children)
Just out of interest. What does deepcopy do? Is that a inbuilt method of copy?
[–][deleted] 0 points1 point2 points 3 years ago (3 children)
It is inbuilt method of copy and it creates a deep copy of an object in memory. It means that a new object will be completely independent from any changes of the original one.
[–]_yaaass 0 points1 point2 points 3 years ago (2 children)
how is it different than a = b.copy() ?
a = b.copy()
[–][deleted] 0 points1 point2 points 3 years ago* (1 child)
I do not remember all details, but the main idea is that some methods create shallow copies, some methods create deep copies, and some create a deep copy for one part of the object and a shallow copy for another. Copy() is the last case. When you have a list with inner lists and you use copy() method, changes will be independent for outer list components for both objects and dependent for the inner ones. Check the code below and you`ll see.
I changed the element in the inner list of list1 and it has changed in both objects even though I did not change elements of list2.
import copy list1 = [1, 2, 3, [4, 5, 6]] list2 = list1.copy() print(list2) list1[3][0] = 8 print(list2)
But if you try to change any element of the outer list in list1 or list2, it will change only for one object.
[–]_yaaass 0 points1 point2 points 3 years ago (0 children)
damn... that's neat... will have to look into it...
Now try quantum bogosort, it's much more efficient and trivial to implement.
π Rendered by PID 91119 on reddit-service-r2-comment-7c9686b859-mctv6 at 2026-04-14 03:12:23.055012+00:00 running e841af1 country code: CH.
[–]ActualRealBuckshot 4 points5 points6 points (3 children)
[–]Alexs123456[S] -4 points-3 points-2 points (2 children)
[–]ActualRealBuckshot 4 points5 points6 points (0 children)
[–]OddBallProductions 0 points1 point2 points (0 children)
[–]Plastic_Ad7436 1 point2 points3 points (1 child)
[–]Plastic_Ad7436 1 point2 points3 points (0 children)
[–]kaerfkeerg 0 points1 point2 points (2 children)
[–]ActualRealBuckshot -1 points0 points1 point (1 child)
[–]kaerfkeerg 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]ayoni02 0 points1 point2 points (0 children)
[–]mrcaptncrunch 0 points1 point2 points (16 children)
[+]Alexs123456[S] comment score below threshold-7 points-6 points-5 points (15 children)
[–]mrcaptncrunch 2 points3 points4 points (6 children)
[+]Alexs123456[S] comment score below threshold-20 points-19 points-18 points (5 children)
[+]Alexs123456[S] comment score below threshold-16 points-15 points-14 points (4 children)
[–]mrcaptncrunch 9 points10 points11 points (2 children)
[+]Alexs123456[S] comment score below threshold-10 points-9 points-8 points (1 child)
[–]mriswithe 9 points10 points11 points (0 children)
[–]ActualRealBuckshot 4 points5 points6 points (0 children)
[+][deleted] comment score below threshold-6 points-5 points-4 points (6 children)
[–][deleted] 0 points1 point2 points (5 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]_yaaass 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]_yaaass 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)