Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Get A Random Set Of Items From From List

Use [TODO: Code shorthand span ] to get random items from a list without repeating any

python
import random

items = [
    'AL', 'AK', 'AS', 'AZ', 'AR', 'CA', 
    'CO', 'CT', 'DE', 'DC']

print(random.sample(items, k=5))
results start

#+OLDNOTES :

import random

# Note that .choices can return the same thing multiple times. # This means it can duplicate. It also means, you can ask for # more items than are in the list.

# .sample() is "without replacement" so each item can be chosen # only one time. If the list is smaller than the requested number # of items (via ` k ` ) then the script goes boom.

def get _ up _ to _ n _ random _ items _ from _ list(source _ list, requested _ number _ of _ items _ to _ get) : items _ to _ get = min(requested _ number _ of _ items _ to _ get, len(source _ list)) return _ list = random.sample(source _ list, k=items _ to _ get)

return return _ list

input _ list = ['AL', 'AK', 'AS', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC']

print( get _ up _ to _ n _ random _ items _ from _ list(input _ list, 5) )