randomly pair items in a list using python

  • Last Update :
  • Techknowledgy :

Your solution limits the possible pairings by splitting the students into two groups, and only taking one from each group. It would be better to just do one shuffle on the whole list of students, then take two at a time to make the pairs.

import random

from itertools
import izip_longest

def grouper(iterable, n, fillvalue = None):
   "Collect data into fixed-length chunks or blocks"
# Taken from itertools recipes:
   # https: //docs.python.org/2/library/itertools.html#recipes
   # grouper('ABCDEFG', 3, 'x') -- > ABC DEF Gxx
args = [iter(iterable)] * n
return izip_longest(fillvalue = fillvalue, * args)

students = ['Joe', 'Sam', 'Bob', 'Jill', 'Paul', 'Eric', 'Chai', 'Chris',
   'Sui', 'Matt', 'Leon', 'Nami', 'Leonard', 'Sai', 'Mike',
   'Julie', 'Jim', 'Holly', 'Marge', 'Ed'
]

random.shuffle(students)

for first_student, second_student in grouper(students, 2):
   print str(first_student), "and", str(second_student)

I had the same problem and used this code to solve it.

import random

students_list_1 = ['Hadi', 'Farzad', 'Amirhossein', 'Yasaman']
students_list_2 = students_list_1.copy()

random.shuffle(students_list_1)
random.shuffle(students_list_2)

while len(students_list_2) > 0:
   if students_list_2[-1] == students_list_1[-1]:
   std2 = students_list_2.pop(0)
std1 = students_list_1.pop()
print(std1, '-', std2)
students_list_1.remove(std2)
students_list_2.remove(std1)
else:
   std2 = students_list_2.pop()
std1 = students_list_1.pop()
print(std1, '-', std2)
students_list_1.remove(std2)
students_list_2.remove(std1)

Suggestion : 2

Last Updated : 30 Jun, 2021,GATE CS 2021 Syllabus

The original dictionary is: {
   'Gfg': 1,
   'best': 3,
   'is': 2
}
The random pair is: ('is', 2)

Suggestion : 3

Your solution limits the possible anycodings_python pairings by splitting the students into anycodings_python two groups, and only taking one from anycodings_python each group. It would be better to just anycodings_python do one shuffle on the whole list of anycodings_python students, then take two at a time to anycodings_python make the pairs.,I'm working on a way to randomly pair up anycodings_python students in my class. Here's what I came up anycodings_python with: ,But how would you do this if you didn't anycodings_python already know the number of students? Any anycodings_python other suggestion on how to improve this?,EDIT: Below is the finished product anycodings_python incorporating several suggestions. I wanted anycodings_python to do this without importing itertools. anycodings_python (Because I want to understand what's going anycodings_python on.)

I'm working on a way to randomly pair up anycodings_python students in my class. Here's what I came up anycodings_python with:

import random

students = ['Joe', 'Sam', 'Bob', 'Jill', 'Paul', 'Eric', 'Chai', 'Chris', 'Sui',
   'Matt', 'Leon', 'Nami', 'Leonard', 'Sai', 'Mike', 'Julie', 'Jim', 'Holly',
   'Marge', 'Ed'
]

random.shuffle(students)

group1 = students[0: 10]
group2 = students[10: ]

combined = zip(group1, group2)

for first_student, second_student in combined:
   print str(first_student), "and", str(second_student)

EDIT: Below is the finished product anycodings_python incorporating several suggestions. I wanted anycodings_python to do this without importing itertools. anycodings_python (Because I want to understand what's going anycodings_python on.)

import random

students = ['Joe', 'Sam', 'Bob', 'Jill', 'Paul', 'Eric', 'Chai', 'Chris', 'Sui',
   'Matt', 'Leon', 'Nami', 'Leonard', 'Sai', 'Mike', 'Julie', 'Jim', 'Holly',
   'Marge', 'Ed'
]

random.shuffle(students)

groups = zip( * [iter(students)] * 2)

for first_student, second_student in groups:
   print str(first_student), "and", str(second_student)

Your solution limits the possible anycodings_python pairings by splitting the students into anycodings_python two groups, and only taking one from anycodings_python each group. It would be better to just anycodings_python do one shuffle on the whole list of anycodings_python students, then take two at a time to anycodings_python make the pairs.

import random

from itertools
import izip_longest

def grouper(iterable, n, fillvalue = None):
   "Collect data into fixed-length chunks or blocks"
# Taken from itertools recipes:
   # https: //docs.python.org/2/library/itertools.html#recipes
   # grouper('ABCDEFG', 3, 'x') -- > ABC DEF Gxx
args = [iter(iterable)] * n
return izip_longest(fillvalue = fillvalue, * args)

students = ['Joe', 'Sam', 'Bob', 'Jill', 'Paul', 'Eric', 'Chai', 'Chris',
   'Sui', 'Matt', 'Leon', 'Nami', 'Leonard', 'Sai', 'Mike',
   'Julie', 'Jim', 'Holly', 'Marge', 'Ed'
]

random.shuffle(students)

for first_student, second_student in grouper(students, 2):
   print str(first_student), "and", str(second_student)

I had the same problem and used this anycodings_python code to solve it.

import random

students_list_1 = ['Hadi', 'Farzad', 'Amirhossein', 'Yasaman']
students_list_2 = students_list_1.copy()

random.shuffle(students_list_1)
random.shuffle(students_list_2)

while len(students_list_2) > 0:
   if students_list_2[-1] == students_list_1[-1]:
   std2 = students_list_2.pop(0)
std1 = students_list_1.pop()
print(std1, '-', std2)
students_list_1.remove(std2)
students_list_2.remove(std1)
else:
   std2 = students_list_2.pop()
std1 = students_list_1.pop()
print(std1, '-', std2)
students_list_1.remove(std2)
students_list_2.remove(std1)

Suggestion : 4

Do not use the array (if you cannot afford it), choose randomly a combination number, check if it wasn't already taken and store it (using a tree container for that),Start with an array containing all the combination numbers, then pick randomly them eventually removing the picked ones from the array,If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.,Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.

N = {
   3,
   8,
   10,
   15,
   16,
   ...
} // size = 30

// Lets say I have already a list with 200 different pairs
my_pairs = {
   (8, 16),
   (23, 32),
   (16, 10),
   ...
}

// Get two random numbers in the list
rn1 = random(N)
rn2 = random(N)

Loop through my_pairs to see
if the pair(rn1, rn2) has already been generated

If there is one, we pick two new numbers rn1 & rn2 at random and retry adding them to my_pairs

If not then we add it to the list
list {
   4,
   8,
   9
}

combination number pair
0(4, 8)
1(4, 9)
2(8, 4)