Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate list of random names - Python

Tags:

python

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
like image 494
BPmuffindrop Avatar asked Jun 07 '26 13:06

BPmuffindrop


2 Answers

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

like image 199
Michael Stachura Avatar answered Jun 09 '26 03:06

Michael Stachura


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
like image 29
Jean-François Fabre Avatar answered Jun 09 '26 03:06

Jean-François Fabre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!