Creating specific combinations using multiple lists

Hi,
I have multiple lists (say ‘n’ number of lists) containing 5 elements each. I want to create unique combinations in a specific pattern as stated in below example. Let’s say i get 3 lists as below.
x = [1,2,3,4,5]
y = [6,17,8,9,10]
z = [11,14,1,16,7]
The combination i’m looking for is that every element of one list should be combined with the last element of the other lists. So, the outcome of the above would be:
unique_combined_lists = [(1,10,7), (2,10,7), (3,10,7), (4,10,7), (5,10,7),
(5,6,7), (5,17,7), (5,8,7), (5,9,7), (5,10,7),
(5,10,11), (5,10,14), (5,10,1), (5,10,16), (5,10,7)]

(5,10,7) should be displayed only once (hence, unique combinations).
Can someone please help? Thanks in advance

Do you know how to get the combinations when duplicates are permitted?

You can use a set to check which combinations have already appeared and should therefore not be added to the result list.

I have used the below that gives me unique combinations.
combinations = list(itertools.product(x,y,z))

However, im unable to figure out a way where i will be able to get the required pattern where every element of one list should be combined with only the last element. It should be an easier part, i guess, but im unable to figure out.

Are you trying to use recursion?

In a way, yes but it doesnt solve the purpose as i dont need each element to be combined with all possible combinations. As stated in my query, I need a specific pattern / combination where recursion wont help (to my knowledge)

You know what you are looking at. You will figure it out. It was just a thought, I was seeing some of that. might want to study that a little as a possibility.
Leonard

You said you have multiple lists. Whenever you have multiple things, the best way to store them is in a list. Therefore, instead of x, y and z, make a list of lists:

lists = [
    [1,2,3,4,5],
    [6,17,8,9,10],
    [11,14,1,16,7],
]

Using itertools.product is clearly the way to go, but you don’t want all the elements of every list, so limit all but one of them to the last element.

You can do that by iterating over lists and making a list of input lists:

for full_list in lists:
    inputs = []

    for sublist in lists:
        if sublist is full_list:
            inputs.append(sublist)
        else:
            inputs.append(sublist[-1 : ])

You can then pass inputs to itertools.product:

result = []

for full_list in lists:
    inputs = []

    for sublist in lists:
        if sublist is full_list:
            inputs.append(sublist)
        else:
            inputs.append(sublist[-1 : ])

    result.extend(itertools.product(*inputs))

Note the use of is to identify which sublist should be left complete.

What remains is to iterate over the result to remove the duplicates. You can do that by using a set to check whether you’ve already seen the tuple.

Alternatively, you could remove the duplicates by iterating over the output of product and using a set.

This helped. Thanks a lot, @MRAB