Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need to generate 1000 unique first name In Python

Tags:

python

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.

like image 779
Abhi Avatar asked Apr 24 '19 18:04

Abhi


People also ask

How do you get unique values in Python?

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.

How do you generate unique strings in Python?

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.

How do you generate a unique random number in Python?

To generate random number in Python, randint() function is used. This function is defined in random module.


2 Answers

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])
like image 179
MatsLindh Avatar answered Oct 15 '22 08:10

MatsLindh


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())
like image 3
OneCricketeer Avatar answered Oct 15 '22 08:10

OneCricketeer