+ 1
{ANSWERED} 1) how to âremoveâ just the second one of the same letter. {ANSWERED} 2)How to âreverseâ just part of a list
List [âaâ âbâ âcâ âaâ] 1) how can I âremoveâ only the second âaâ and not the first âaâ (I just learned the remove function)and can take out the second without the first 2) can I âreverseâ just âaâ and âbâ and not the rest
8 Respostas
+ 1
1. del statement lets you choose item to remove by list index. You can first search the right occurrence, then use it's index.
2. I don't know if that got clear, but I mean assign only to the items to swap: list[first index], list[second index] = list[second index], list[first index]
+ 1
Do you need that specifically for this list, or a general approach, being this list an example?
1) Use del statement
2) A simple assignment, as in a, b = b, a
Knowing better your need, we'll proceed.
+ 1
Emerson Prado
Its an example not specifec
+ 1
Emerson Prado
For question 1
When i use the remove function it goes to the first one guessing that ill have the same problom by del function whan to take out only the second one
For question 2
Just wanted to know if there is a proper way to do it and not reasighn it
+ 1
Emerson Prado
So answer to question 1 if i want to del the second âaâ i do this
List [âaâ âbâ âaâ ]
del (list[2])
Print (list)
Output
a b
Answer to question 2 i have to reassign the âaâ âbâ cant use the reverse function
Please let me know if i understood had a hard time understanding
+ 1
You understood correctly.
For the del statement, that's just it. The only correction is you don't need parenthesis - it's a statement, not a function.
The reverse function only reverses the whole list so, yes, it can't be used here.
+ 1
So i do it
del list [2]
0
Emerson Prado
Thanks