Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combinations on a list in python

So I got a list in python like this:

[1,2,3,4]

And I want the combinations between the number 3 with every number something like this:

[(1,3),(2,3),(3,4)]

Is there something I can use? I know there is something called itertools, but I'm kinda new so i'm not sure how to use it.

Thank you!

like image 802
Carlos Eduardo Corpus Avatar asked Dec 05 '25 20:12

Carlos Eduardo Corpus


1 Answers

You might want to use a list comprehension:

orig_list = [1,2,3,4]
[(entry, 3) for entry in orig_list if entry != 3] # [(1, 3), (2, 3), (4, 3)]

If you're not interested in duplicate values you can make it into a set:

orig_list = set([1,2,3,4])
[(entry, 3) for entry in orig_list if entry != 3] # [(1, 3), (2, 3), (4, 3)]
like image 119
Carsten Avatar answered Dec 08 '25 11:12

Carsten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!