+ 1
remove and revers
how to remove and reverse, please explain with examples?
1 Resposta
0
Using you can use slicing to reverse a list or string like:
string = "hello"
print(string[::-1])
>>> "olleh"
The same applies for lists
To remove an item from a list, you can use pop() or remove()
pop() removes an element by index. For example:
ls = [1, 2, 3, 4, 5]
ls.pop(1)
>>> [1, 3, 4, 5]
remove() removes an element by value. For example:
ls = [1, 2, 3, 4, 5]
ls.remove(1)
>>> [2, 3, 4, 5]
Hope this helps😁🐍