Here you say you want to remove from the list of people all who were invited. So a very simple way (assuming there are no repeat people) is:
pessoas = ["Matheus", "Maria", "Felipe", "Tulio", "Karen"]
convidados = ["Felipe", "Karen", "Matheus", "Tulio"]
for convidado in convidados:
try:
pessoas.remove(convidado)
except ValueError: # convidado não está na lista de pessoas
pass
print(pessoas) # ['Maria']
I took care to capture the ValueError
, because if the guest does not exist in the list of people, this exception is cast. In case, I am ignoring the error and going to the next guest (also changed the name of the list to pessoas
- plural - since it is a list with several people; it may seem like a silly detail, but give names better help when programming).
The problem is that pessoas.remove(convidado)
only removes the first occurrence of the guest, then the above code only works if the list of people does not have repeated names. For example, if she were:
pessoas = ["Matheus", "Maria", "Felipe", "Tulio", "Karen", "Felipe"]
Only the first "Philip" would be removed, and the result would be ['Maria', 'Felipe']
.
Another detail is that this code modifies the list of people. If you do not want this, an alternative is to create another list:
pessoas = ["Matheus", "Maria", "Felipe", "Tulio", "Karen", "Felipe", "Fulano", "Ciclano"]
convidados = ["Felipe", "Karen", "Matheus", "Tulio"]
pessoas_nao_convidadas = [ p for p in pessoas if p not in convidados ]
print(pessoas_nao_convidadas) # ['Maria', 'Fulano', 'Ciclano']
In this case, in addition to creating another list, I also deleted the repetitions by checking if the person is not on the guest list with not in
.
If you keep people in order nay for important, you can also use set
, which is a structure that does not allow repeatable elements. Thus, simply turn the lists into set
's, subtract each other and convert the result into a list:
pessoas = ["Matheus", "Maria", "Felipe", "Tulio", "Karen", "Felipe", "Fulano", "Ciclano"]
convidados = ["Felipe", "Karen", "Matheus", "Tulio"]
# retorna uma lista com 'Maria', 'Fulano' e 'Ciclano', mas não necessariamente nesta ordem
pessoas_nao_convidadas = list(set(pessoas) - set(convidados))
So I want to use at least two if chained
What for? No need, the above solutions are much simpler, your code was complicating for no reason.
All right that solution with not in
should do a linear search on the list, so it is an "implicit loop" within a for
, but the point is you don’t need to use two for
explicit chained up.
Yes it is a solution, one of the ways that tbm did not want to use is a new list.
– Allan Belem
I edited and posted a new solution, see if it suits you...
– Lucas Bittencourt
Should be removed from the person list the people who were invited, should result the two lists, showing the person list without the chosen guests. My code can remove but does not have a stop condition.
– Allan Belem
I got it, I edited it again with a new solution, see if it suits you :)
– Lucas Bittencourt
I understand is that I would love to follow the same structure keeping the code, I even managed to remove when there is more than 1 person, but I would like something to return the index to 0, so I can remove who is left.
– Allan Belem