Hmm, sounds like homework, but I'll bite.
The underlying problem does sound like homework, but the OP
posted a working solution, and was only looking for ways to
improve it. So I'm a little more lenient on providing
alternatives. It's true that the homework problem may have been
exactly as
> I had a task in a book to pick 5 items from a list of 26 ensuring the
items are not repeated
>
>
> import random
> list = ['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']
> word = ' '
> a = random.choice(list)
> list.remove(a)
> b
I had a task in a book to pick 5 items from a list of 26
ensuring the items are not repeated
import random
list = ['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']
word = ' '
a = random.choice(list)
list.remove(a)
b = random.choice(
If the list is unique of 26 elements is guaranteed to be unique, simply:
>
Wow, 6am copy editing of my own posts is terribly ineffective.
"If the list of 26 elements is guaranteed to be unique"
--
http://mail.python.org/mailman/listinfo/python-list
2009/1/29 garywood :
> I had a task in a book to pick 5 items from a list of 26 ensuring the items
> are not repeated
>
>
> import random
> list = ['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']
> word = ' '
> a = random.choice(li
On Thu, Jan 29, 2009 at 6:11 AM, garywood wrote:
> I had a task in a book to pick 5 items from a list of 26 ensuring the
> items are not repeated
>
>
If the list is unique of 26 elements is guaranteed to be unique, simply:
>>> import random
>>> random.sample(list, 5)
['g', 'y', 'i',