+ 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

27th Apr 2022, 11:16 PM
Lz1234
8 ответов
+ 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]
28th Apr 2022, 12:19 AM
Emerson Prado
Emerson Prado - avatar
+ 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.
27th Apr 2022, 11:35 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Emerson Prado Its an example not specifec
27th Apr 2022, 11:37 PM
Lz1234
+ 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
27th Apr 2022, 11:38 PM
Lz1234
+ 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
28th Apr 2022, 3:09 AM
Lz1234
+ 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.
28th Apr 2022, 10:55 AM
Emerson Prado
Emerson Prado - avatar
+ 1
So i do it del list [2]
28th Apr 2022, 6:35 PM
Lz1234
28th Apr 2022, 6:36 PM
Lz1234