1

I have simple list:

simple_list = [
   SimpleObject(name=faker.name(), city=faker.city())
   for i in range(0, 10)
]

This gives me for example:

simple_list = [
   SimpleObject(name="aaa", city="London"),
   SimpleObject(name="bbb", city="Madrid"),
   SimpleObject(name="ccc", city="Paris"),
   SimpleObject(name="ddd", city="Madrid"),
]

How to ensure the uniqueness of this list using the city key? In my example, I would like to receive:

simple_list = [
   SimpleObject(name="aaa", city="London"),
   SimpleObject(name="bbb", city="Madrid"),
   SimpleObject(name="ccc", city="Paris"),
]

or

simple_list = [
   SimpleObject(name="aaa", city="London"),
   SimpleObject(name="ccc", city="Paris"),
   SimpleObject(name="ddd", city="Madrid"),
]

It doesn't matter to me which ones, it's important that the cities don't repeat themselves.

khelwood
  • 55,782
  • 14
  • 81
  • 108
peyih
  • 19
  • 1
  • @Mark - The `faker` tag wasn't added by the OP, even though the OP is using faker in this [mcve], that doesn't necesarily mean the actual code does. The op's question is above making a list unique based off an attribute of the elements. – Sayse Nov 29 '21 at 16:49
  • Yes @Sayse, but the correct answer is below for this question. That answer will never appear on the dupe you mentioned. This question and the answer below provides unique value to future users. – Mark Nov 29 '21 at 16:50
  • OP, are you asking how to make unique values with faker, or to make a unique list? – Sayse Nov 29 '21 at 16:50
  • @Mark - I agree that the below answer is a good answer if thats what is being asked, but that is still different to what is written – Sayse Nov 29 '21 at 16:51
  • It doesn't really matter what the OP intends or who added the `faker` tag. What matters is that this is valuable to future users. Perhaps asking the OP to clarify in the title will improve it even more for the future. – Mark Nov 29 '21 at 16:52
  • @Mark - Well then theres the [existing duplicate](https://stackoverflow.com/questions/55836752/i-need-to-generate-1000-unique-first-name-in-python/55836950) to what is proposed as the real question here where the accepted answer is to use a [provider](https://faker.readthedocs.io/en/master/providers/faker.providers.address.html#faker.providers.address.Provider.city) (or unique as a seperate answer). Its not up to us to change the OP's original question into one that provides value – Sayse Nov 29 '21 at 16:56

1 Answers1

2

You can do this using a Faker instance with the unique attribute:

from faker import Faker
fake = Faker()

simple_list = [
   SimpleObject(name=fake.name(), city=fake.unique.city())
   for i in range(0, 10)
]

Note that Faker will raise an exception if it can't find a new unique value. From the docs:

Note, to avoid infinite loops, after a number of attempts to find a unique value, Faker will throw a UniquenessException. Beware of the birthday paradox, collisions are more likely than you’d think.

https://faker.readthedocs.io/en/master/#unique-values

match
  • 10,388
  • 3
  • 23
  • 41