I need to generate 1000 unique first names and store them in a list. I am using Python faker but getting so many repeated values.
import random
from random import shuffle
from faker import Faker
fake = Faker()
fake.random.seed(4321)
first_n=[]
for i in range(1000):
name=fake.first_name()
if name in first_n:
first_n.append("Repeat")
else:
first_n.append(name)
Thanks for your help.
Python numpy. unique() function To Create a List with Unique Items. Python NumPy module has a built-in function named, numpy. unique to fetch unique data items from a numpy array.
In order to generate random strings in Python, we use the string and random modules. The string module contains Ascii string constants in various text cases, digits, etc. The random module on the other hand is used to generate pseudo-random values.
To generate random number in Python, randint() function is used. This function is defined in random module.
Instead of generating random values and then checking if they're unique, retrieve the list of the names stored in the provider you're using, shuffle them and return the first 1000. That way you won't run into collisions at all. There's about 7k first names defined in the en
provider, while the other languages may have far less - making collisions rather certain as you're getting further into the sequence.
from random import shuffle, seed
from faker.providers.person.en import Provider
first_names = list(set(Provider.first_names))
seed(4321)
shuffle(first_names)
print(first_names[0:1000])
Youll want to store data in a set instead of a list to prevent duplicates
Then you can use a while loop
first_n = set()
while len(first_n) < 1000:
first_n.add(fake.first_name())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With