Created
February 25, 2026 10:49
-
-
Save jelc53/4ee087b4956e26ee50cdac025a65eea0 to your computer and use it in GitHub Desktop.
fun recursion example to generate all combination sets of a list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def power_set(my_list): | |
| if len(my_list) == 0: | |
| return [[]] | |
| power_set_without_first = power_set(my_list[1:]) | |
| with_first = [ [my_list[0]] + rest for rest in power_set_without_first ] | |
| return with_first + power_set_without_first | |
| universities = ['MIT', 'UCLA', 'Stanford', 'NYU'] | |
| power_set_of_universities = power_set(universities) | |
| for set in power_set_of_universities: | |
| print(set) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment