8

I'm trying to create a list of names, with each one being different. Here is my code right now, but all it does it create multiple instances of the same name.

import random

first_names=('John','Andy','Joe')
last_names=('Johnson','Smith','Williams')

full_name=random.choice(first_names)+" "+random.choice(last_names)

group=full_name*3

For example, this would show up as:

John Smith
John Smith
John Smith

But I want something like:

John Williams
Andy Johnson
Joe Johnson

10 Answers 10

11

Why you complicate such a simple task :)

Maybe this will help: https://faker.readthedocs.io/en/master/index.html

from faker import Faker
fake = Faker()

for i in range(0, 10):
    print(fake.name())

Faker has a lot of usefull providers you can use: https://faker.readthedocs.io/en/master/providers.html

Sign up to request clarification or add additional context in comments.

Comments

7

you're just duplicating your string here. Random occurs only once.

Do it in a generator comprehension instead, and join the results with space:

import random

first_names=('John','Andy','Joe')
last_names=('Johnson','Smith','Williams')

group=" ".join(random.choice(first_names)+" "+random.choice(last_names) for _ in range(3))


print(group)

outputs:

Joe Williams Joe Johnson Joe Smith

Comments

4

This is because you generated one name and then replicated it three times.

If you want three different names, then loop through your choice routine three times:

group = []
for i in range(3):
    full_name=random.choice(first_names)+" "+random.choice(last_names)
    group.append(full_name)
 #assuming he wants at least some kind of seperator between the names.
 group_string = ", ".join(group)

BTW, do you really want group to be all the names just concatenated?

Comments

1

Since the output is on one line with spaces separating each name, you can simplify the code and store every name in one list, albeit you append names in the "first name" "last name" pattern. From there you can print 6 names at the end all seperated by a space, giving you three full names. In all, the 'random.choice()' function will need to be called 6 times.

import random

first_names=('John','Andy','Joe')
last_names=('Johnson','Smith','Williams')
names = []
for i in range(3):
    names.append(random.choice(first_name)
    names.append(random.choice(last_name)

print ' '.join(names)

output

Andy Smith Andy Williams John Johnson

Comments

0

What you did is use *3 on a string which means it will copy that string three times.

To do what you want, you have to call random.choice for first name and for last name three times.

full_name1 = random.choice(first_names)+" "+random.choice(last_names)
full_name2 = random.choice(first_names)+" "+random.choice(last_names)
full_name3 = random.choice(first_names)+" "+random.choice(last_names)

group = full_name1 + " " + full_name2 + " " + full_name3

You can also do that in a for loop to not duplicate the code.

Comments

0

you need to invoke random.choice() 3 times other than invoking once and print the same fullname 3 times

import random

first_names=('John','Andy','Joe')
last_names=('Johnson','Smith','Williams')
group = list()
for x in range(3):
    fullname = random.choice(first_names)+" "+random.choice(last_names)
    group.add(fullname)
print " ".join(group)

Comments

0

... just shorter.

from random import choice
first_names=('John','Andy','Joe')
last_names=('Johnson','Smith','Williams')

name = " ".join(["%s %s" % (choice(first_names), choice(last_names)) for _ in range(3)])    

Comments

0

You could put the main part in a function with a print in it and then call the function 3 times.

def name():
    import random
    
    first_names=('John','Andy','Joe')
    last_names=('Johnson','Smith','Williams')
    
    full_name=random.choice(first_names)+" "+random.choice(last_names)
    print(full_name)
for _ in range(3):
    name()

Comments

0

pip install names

Create random names with python Then to create random names just do:

import names

for i in range(10):
    print(names.get_full_name())

Comments

0
# 1)

import random

first_names=('John','Andy','Joe')
last_names=('Johnson','Smith','Williams')

full_name = ((random.choice(first_names) + " " + random.choice(last_names))for i in range(len(first_names) * len(last_names)))

group = set(full_name) # use the set to avoid displaying duplicate names

for name in group:
    print(f"*{name}*")

print("\n","---***---","\n")


# 2) or

def name_generator(limit):
    for _ in range(limit):
        yield random.choice(first_names) + " " + random.choice(last_names)

gen = name_generator(3)

for i in gen:
    print(i)

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.