Python Forum
Sample random, unique string pairs from a list without repetitions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sample random, unique string pairs from a list without repetitions
#1
Hi everyone,

This is my first post on this forum so please bear with me. At the very beginning I'd like to let you know that I'm a complete rookie and before you start hammering me that I haven't tried to solve the problem myself, that it's basics etc. please know that I did some research into this kind of a problem, mainly on how to extract unique pairs from a list however most of the solutions I've come across are a bit too complex for me to follow and had a bit different application as they were strictly integer based rather than string based. I literally just started my adventure with coding.

Problem:
I want to write a program which will select two random names from a list and by doing so will create a unique pair of names. The two people selected will be nominated to buy Christmas presents for each other. We always have a problem in my family when it comes to buying Christmas presents so I thought it'd be nice to randomize things a little.

import random

names = ["Adam","Anna","Jessica","Stephen","Claude","Gary"]


for i in names:
    k = random.sample(names, 2)
    j =k
    if j == k:
        k = random.sample(names,2)
    print(k) 
The main problem now is that I don't know how to create a proper check loop which would compare the newly generated pairs with the names stored in the original list so I can avoid having the same or repetitive pairs. For that reason I used the if statement to generate another set of pairs but I realise it is not the proper way of doing it, therefore any guidance on how proceed would be much appreciated.

Thanks in advance.
Reply
#2
Two approaches to solving:

Shuffle the names like a deck of cards and remove names two at a time. This is the easiest solution. You can use random.shuffle(names) or names = random.sample(names, len(names)) to do the shuffling.

Randomly pick names from the list like picking cards from a fan. Remove the picked name from the list just like you remove the card from the fan. This is closer to what you were doing.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Improve Prime Pairs Code jzakiya 7 1,458 Mar-09-2025, 08:19 PM
Last Post: jzakiya
  Entry field random pull from list, each return own line Bear1981 6 724 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
Star Pairs Trading Simulation Kiitoos 2 1,825 Aug-21-2024, 09:43 AM
Last Post: Larz60+
  find random numbers that are = to the first 2 number of a list. Frankduc 23 6,973 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 5,539 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 2,752 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  random.choices and random.sample azizrasul 5 6,086 Sep-16-2022, 08:10 PM
Last Post: deanhystad
  How to get unique entries in a list and the count of occurrence james2009 5 4,125 May-08-2022, 04:34 AM
Last Post: ndc85430
  Pairs of multiplied prime number--->N Frankduc 13 5,894 Jan-16-2022, 01:52 PM
Last Post: Frankduc
  generating random string unique forever Skaperen 5 4,303 Aug-20-2021, 07:15 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020