How can I generate a random value and then use the pop method to remove it?

Viewed 69

I am trying to take a random name from a list and then once it has been printed, I want to remove it from that list so that it isn't used ever again. I want to use the pop method but I'm not sure how to take a random name from the list since the pop method (to my knowledge) only accepts integers.

Here is my code:

Boy = ['David','John','Paul','Mark','James','Andrew','Scott','Steven','Robert','Stephen','William','Craig','Michael'
       ,'Stuart','Christopher','Alan','Colin','Kevin','Gary','Richard','Derek','Martin','Thomas','Neil','Barry',
       'Ian','Jason','Iain','Gordon','Alexander','Graeme','Peter','Darren','Graham','George','Kenneth','Allan',
       'Simon','Douglas','Keith','Lee','Anthony','Grant','Ross','Jonathan','Gavin','Nicholas','Joseph','Stewart',
       'Daniel','Edward','Matthew','Donald','Fraser','Garry','Malcolm','Charles','Duncan','Alistair','Raymond',
       'Philip','Ronald','Ewan','Ryan','Francis','Bruce','Patrick','Alastair','Bryan','Marc','Jamie','Hugh','Euan',
       'Gerard','Sean','Wayne','Adam','Calum','Alasdair','Robin','Greig','Angus','Russell','Cameron','Roderick',
       'Norman','Murray','Gareth','DeanEric','Adrian','Gregor','Samuel','Gerald','Henry','Benjamin','Shaun','Callum',
       'Campbell','Frank','Roy','Timothy','Liam','Noah','Oliver','William','Elijah','James','Benjamin','Lucas',
       'Mason','Ethan','Alexander','Henry','Jacob','Michael','Daniel','Logan','Jackson','Sebastian','Jack','Aiden',
       'Owen','Samuel','Matthew','Joseph','Levi','Mateo','Wyatt','Carter','Julian','Luke','Grayson','Isaac','Jayden'
       ,'Theodore','Gabriel','Anthony','Dylan','Leo','Christopher','Josiah','Andrew','Thomas','Joshua','Ezra',
       'Hudson','Charles','Caleb','Isaiah','Ryan','Nathan','Adrian','Christian']

Girl = ['Emma','Ava','Sophia','Isabella','Charlotte','Amelia','Mia','Harper','Evelyn','Abigail','Emily','Ella',
        'Elizabeth','Camila','Luna','Sofia','Avery','Mila','Aria','Scarlett','Penelope','Layla','Chloe','Victoria',
        'Madison','Eleanor','Grace','Nora','Riley','Zoey','Hannah','Hazel','Lily','Ellie','Violet','Lillian','Zoe',
        'Stella','Aurora','Natalie','Emilia','Everly','Leah','Aubrey','Willow','Addison','Lucy','Audrey','Bella',
        'Nova','Brooklyn','Paisley','Savannah','Claire','Skylar','Isla','Genesis','Naomi','Elena','Caroline','Eliana'
        ,'Anna','Maya','Valentina','Ruby','Kennedy','Ivy','Ariana','Aaliyah','Cora','Madelyn','Alice','Kinsley',
        'Hailey','Gabriella','Allison','Gianna,Sarah','Autumn','Quinn','Eva','Piper','Sophie','Sadie','Delilah'
        ,'Josephine','Nevaeh','Adeline','Arya','Emery','Lydia','Clara','Vivian','Madeline','Peyton','Julia','Rylee',
        'Brielle','Reagan','Natalia','Jade'',Athena','Maria','Leilani','Everleigh','Liliana','Melanie','Mackenzie',
        'Hadley','Raelynn','Kaylee','Rose','Arianna','Isabelle','Melody','Eliza','Lyla','Katherine','Aubree',
        'Adalynn','Kylie','Faith','Marly','Margaret','Ximena','Iris','Alexandra','Jasmine','Charlie','Amaya',
        'Taylor','Isabel','Ashley','Khloe','Ryleigh','Alexa','Amara','Valeria','Andrea','Parker','Norah','Eden',
        'Elliana','Brianna','Emersyn','Valerie','Anastasia','Eloise','Emerson','Cecilia','Remi','Josie','Reese',
        'Bailey','Lucia','Adalyn','Molly','Ayla','Sara','Daisy','London','Jordyn','Esther','Genevieve','Harmony',
        'Annabelle','Alyssa','Ariel','Aliyah','Londyn','Juliana','Morgan','Summer','Juliette','Trinity','Callie',
        'Sienna','Blakely','Alaia','Kayla','Teagan','Alaina','Brynlee','Finley','Catalina','Sloane','Rachel','Lilly'
        ,'Ember']

def boyname():
    result = Boy.pop(insert code for random name here)
    print(result)
    print(Boy)

boynames = boyname()
print(boynames)
5 Answers

Simply:

name = Boy.pop(random.randint(0, len(Boy)-1))

But: you should not do that, especially from within a function, as it affects the caller's list (or in your case, the global variable containing the list).

Consider instead the ability to take a random subset (without side effects):

sample = random.sample(Boy, k)

For example, you can go through a random ordering of your list (i.e. sample without replacement), ensuring you will "print" each value just once:

for name in random.sample(Boy, len(Boy)):
   ...

You can even turn this into a generator, for infinite fun:

from itertools import count

def pick(a, n_loops=None):
    """if n_loops is None: cycle endlessly"""
    counter = count()
    while n_loops is None or next(counter) < n_loops:
        yield from random.sample(a, len(a))

Usage:

name_gen = pick(Boy)  # infinite generator: loops around shuffles;
                      # no repeat during a single loop
list(islice(name_gen, 5))

# or

names = pick(Boy, 1)  # one random shuffle of Boy

Another fun example:

for b, g in islice(zip(pick(Boy), pick(Girl)), 4):
    print(f'{b} and {g}')
# out:
Carter and Molly
Malcolm and Lydia
Sebastian and Ruby
Charles and Aliyah

Try this code

from random import choice

def boyname():
    global Boy
    result = choice(Boys)
    print(result)
    Boy.remove(result)
    print(Boy)

boyname()

Something like this would work:

import numpy as np
np.random.seed(1)

def boyname():
    """
    Returns a random `boy` from `Boy` (list) while removing from list
    """
    idx = np.random.randint(0,len(Boy)-1)
    res = Boy.pop(idx)
    return res

my_boy = boyname()
print(my_boy)

Can use this

import random
def boyname():
    result = Boy.pop(Boy.index(random.choice(Boy)))
    print(result)
    print(Boy)
    return 'End of Program'

boynames = boyname()
print(boynames)

In my opinion, you can try to get the index of a name by random function, and you can define the function like this:

import random
def boyname():
    boy_name_list_length = len(Boy)
    index_of_name = int(random.random() * boy_name_list_length)
    result = Boy.pop(index_of_name)
    print(result)
    print(Boy)

And call the function like this:

boyname()

Remember just run the Boy and Girl list initialization once!

Related