[Tutor] Random list exercise

lists lists at justuber.com
Fri Sep 10 00:22:14 CEST 2010

>>> Hi tutors,
>>> Still on my Python learning journey! I've just competed an exercise
>>> which asks the student to "Create a program that creates a list of
>>> words in random order. This program should print all the words and not
>>> repeat any." I've printed the list for my own needs. The list
>>> randwords aims to answer the specific request of the exercise author.
>>> If anyone has the inclination and a minute to spare, please run your
>>> eyes over my answer. It works, but is it an OK way to approach the
>>> exercise?
>>> Thanks again :-D
>>> Chris
>>> import random
>>> #LIST
>>> words = ["one", "two", "three", "four", "five", "six", "seven",
>>> "eight", "nine", "ten", "eleven"]
>>> randwords = []
>>> while words: #has entries in it
>>>    wordslen = len(words) #get the length of the list
>>>    index = random.randint(0, wordslen -1) #get a random index
>>>    chosenword = words[index]
>>>    randwords.append(chosenword) #append the random word to a new list
>>>    del words[index] #del the word from the old list
>>> for word in randwords:
>>>    print word # print them
>> Several small and not so small points:
>> 1. you assign wordslen each pass through your loop.  While it doesn't
>> matter in a small loop, it wastes time on the order of the size of your
>> list.  Instead move wordslen = len(...  above your while loop.  Any time you
>> put code in a loop that doesn't change in each iteration, you should move it
>> out of the loop.
>> 2. since you only use chosenword once in your loop, you could remove
>> chosenword = words[index] line and replace chosenword in the append(... with
>> words[index]
>> 3. your list doesn't contain any duplicate words.  Since  your program is
>> supposed to catch this, you should add a duplicate to see if it works.
>> (No!)
>> 4. I think your line del words[index] is supposed to help out with item 3
>> but it doesn't.  It just removes the word you just used selected.
>> 5. And finally, I think you want to print
>> Just minor points.  nice job -- getting there
>> --
>> Joel Goldstick
> I looked into this a little more, and although I'm ok with my comments, I
> did some experimenting and looked into the stuff in random.  I came up with
> this.  It may not be helpful since you may be learning about loops and
> randint, but this works:
> import random
> words = ["one", "two", "three", "four", "five", "six", "seven",
>                     "eight", "four", "nine", "ten", "eleven"]
> word_set = list(set(words))    # this removes duplicates since set contains
> one of each value.  Then convert back to a list
> print "original words: ", words    # just wanted to see my original list
> print "removed duplicates with set:", word_set   # this shows that I don't
> have duplicates
> random.shuffle(word_set)  # this shuffles the list in place.  Now word_set
> is shuffled (randomized!)
> print "randomized: ", word_set   # see!

Hey Joel,

I've not done sets yet.

I guess part of the problem when you're a noob like myself is that
there is always going to be a more efficient way of doing something
(but just that I haven't learnt it yet) :-D

Looking at the original code again, I think that the wordslen =
len(words) bit might have to stay in the loop. The value of wordslen
changes at each iteration of the loop you see.

The reason I'm removing one entry from the words list at each
iteration is to ensure that all of the words are included in randwords
(although again, this probably isn't the best way to do it!).

I think I may need to add something like if words[index] not in
randwords: to make sure that the word isn't already in the list I want
to jumble up' and print perhaps?




More information about the Tutor mailing list