'How can I print all lists without each item using for loops, and is there an alternative in python?
I am trying to understand how this for loops works exactly. I have a list of letters (first_list = ["A","B","C"]), and I want to print all the possible lists without each letter (["B","C"]; ["A","C"], ["A","B"]). I have this loop, but I do not understand why it does not work:
first_list = ["A","B","C"]
for i in first_list:
second_list = first_list
second_list.remove(i)
print(second_list)
--> ['B', 'C']
['B']
On the other hand I have this loop, and it works, but I also do not understand why it works, and what is the difference in both loops
first_list = ["A","B","C"]
for i in first_list:
first_list = ["A","B","C"]
second_list = first_list
second_list.remove(i)
print(second_list)
-->['B', 'C']
['A', 'C']
['A', 'B']
Can you help me understand how these two for loops work, and is there another way to have this output (with something other than a for loop)?
Solution 1:[1]
This happens because python assigns references simply it assigns addresses instead of values when you do this second_list = first_list address of ["A","B","C"](this is a object and has a address) is assigned to second_list variable. so now both first_list and second_list point to same address which means change in any variable reflects in others until any of the variable is pointed towards other object, first_list = ["A","B","C"] every time you do this new object gets created and it's address is assigned to that particular variable so it works in the second code. refer to this link for detailed explanation how reference works
first_list = ["A","B","C"]
for i in first_list:
second_list = first_list.copy()
second_list.remove(i)
print(second_list)
Solution 2:[2]
If you would like to get all possible combinations for a list your can use itertools.combinations!
import itertools
[combination for combination in itertools.combinations(["A","B","C"], 2)]
Will give the following result:
[('A', 'B'), ('A', 'C'), ('B', 'C')]
Solution 3:[3]
In the first code snippet, you set second_list to point to the same location in memory as first_list. Thus, changes (i.e. deletions) to second_list will also be reflected in first_list. In the second code snippet, you re-initialize first_list to ["A","B","C"] at the beginning of every iteration of the for loop. In this case, you're operating on a fresh list for every iteration.
You'll need to use some sort of iteration structure to determine which element to remove for a given iteration, but you can make it more concise using a list comprehension:
first_list = ["A","B","C"]
for skip_index in range(len(first_list)):
print([elem for index, elem in enumerate(first_list) if index != skip_index])
Solution 4:[4]
If you remove the re-used local variable first_list, it becomes easier to understand:
for i in ["A", "B", "C"]:
selections = ["A", "B", "C"]
selections.remove(i)
print(selections)
This is a combinatorial program (3 pick 2). You can produce this output using python itertools combinations (https://docs.python.org/3.1/library/itertools.html?highlight=combinations#itertools.combinations)
>>> from itertools import combinations
>>> list(combinations(['A', 'B', 'C'], 2))
[('A', 'B'), ('A', 'C'), ('B', 'C')]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ruud van den Boomen |
| Solution 3 | BrokenBenchmark |
| Solution 4 | Bemis |
