i’ll reply here just for the sake of finishing the conversation. the example is from the same exercise.
there is a list of student with a favourite subject:
math_students = [student_seats.get_students_with_subject("Math")]
print(math_students)
output: [[(‘Karina M’, ‘Math’), (‘Benny D’, ‘Math’), (‘Marisol R’, ‘Math’)]]
science_students = [student_seats.get_students_with_subject("Science")]
print(science_students)
output: [[(‘Alex C’, ‘Science’), (‘Trudy B’, ‘Science’)]]
math_science_students = itertools.chain(math_students, science_students)
print(list(math_science_students))
output: [[(‘Karina M’, ‘Math’), (‘Benny D’, ‘Math’), (‘Marisol R’, ‘Math’)], [(‘Alex C’, ‘Science’), (‘Trudy B’, ‘Science’)]]
so, there are 5 students: 3 math and 2 science
so there enough elements in the list to make combinations of 4, but
math_science_quads = itertools.combinations(math_science_students, 4)
print(list(math_science_quads))
output: [ ]
this approach:
math_science_quads = student_seats.get_students_with_subject("Math") + student_seats.get_students_with_subject("Science")
print(list(itertools.combinations(math_science_quads, 4)))
results in this output: [((‘Karina M’, ‘Math’), (‘Benny D’, ‘Math’), (‘Marisol R’, ‘Math’), (‘Alex C’, ‘Science’)), ((‘Karina M’, ‘Math’), (‘Benny D’, ‘Math’), (‘Marisol R’, ‘Math’), (‘Trudy B’, ‘Science’)), ((‘Karina M’, ‘Math’), (‘Benny D’, ‘Math’), (‘Alex C’, ‘Science’), (‘Trudy B’, ‘Science’)), ((‘Karina M’, ‘Math’), (‘Marisol R’, ‘Math’), (‘Alex C’, ‘Science’), (‘Trudy B’, ‘Science’)), ((‘Benny D’, ‘Math’), (‘Marisol R’, ‘Math’), (‘Alex C’, ‘Science’), (‘Trudy B’, ‘Science’))]
i made an example, which also returns empty:
numbers = [4, 7, 2, 9, 4, 6, 1, 9, 4]
letters = ['s', 'f', 'h', 'e', 'u']
nls = itertools.chain(numbers, letters)
print(list(nls))
nls_combos = itertools.combinations(nls, 4)
print(list(nls_combos))
this also prints nothing (with and without list():
nls_combos = list(itertools.combinations(nls, 4))
for combo in nls_combos:
print(combo)
this works fine, so i wonder if the using chain before combinations is an issue:
numbers = [4, 7, 2, 9, 4, 6, 1, 9, 4]
test_combo = itertools.combinations(numbers, 4)
for test in test_combo:
print(test)