#๐Ÿ”’ Simple coding that everyone can know I think but not me so dont hesitate to help

24 messages ยท Page 1 of 1 (latest)

candid path
#

I would like a code that returns a list without the last number, can you help me pls ? For the moment I did this :

carmine owlBOT
#

@candid path

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wary crypt
#

!slice

carmine owlBOT
#
Sequence slicing

Slicing is a way of accessing a part of a sequence by specifying a start, stop, and step. As with normal indexing, negative numbers can be used to count backwards.

Examples

>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters[2:]  # from element 2 to the end
['c', 'd', 'e', 'f', 'g']
>>> letters[:4]  # up to element 4
['a', 'b', 'c', 'd']
>>> letters[3:5]  # elements 3 and 4 -- the right bound is not included
['d', 'e']
>>> letters[2:-1:2]  # Every other element between 2 and the last
['c', 'e']
>>> letters[::-1]  # The whole list in reverse
['g', 'f', 'e', 'd', 'c', 'b', 'a']
>>> words = "Hello world!"
>>> words[2:7]  # Strings are also sequences
"llo w"
wary crypt
#

Please read embedded above. I think thatโ€™ll answer your question, but if not clear, ask more

candid path
#

ok thank you

#

it is a way to do it

#

but do you know why my code doesn't work ?

wary crypt
#

It works fine at removing the first occurrence of the last digit

candid path
#

but the "2" is still there

wary crypt
#

Read what I wrote

candid path
#

ahhhhhh

#

sorry I missread

#

do you know what I should do ?

#

because I don't understand why

#

I put [-1]

#

so it should be the last two

deep nova
#

You want all 2s gone? (If the last digit is 2?) Or just the last element

#

?

#

You can use a slice to get a new string without the last char

#

Then turn that string to a list

real token
#

[-1] is the last and it is a 2.
Then you are looking through the list removing it the first time it is found.

letters = ['a', 'b', 'c', 'd']
print( letters[:-1] )
['a', 'b', 'c']

carmine owlBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.