Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

270
Vistas
Syntax for returning the entire list when using the minus sign?

Python lists have nifty indexing/slicing capabilities. Here are several examples:

x = "123456"

x[:-3]
'123'

x[:-1]
'12345'  # -1 slices off last element

x[:-0]  # -0 slices off .. everything .. this is what i'd like to fix
''

I would like to slice off a variable number of elements d:

x[:-d]

But if d were 0 we get a much different result than desired. A workaround is:

d = 0
x[:-d if d else len(x)]
'123456'

That is possible - but is there any [shorter] alternative?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

The issue you're hitting is that -0 is the same as 0, and x[0:0] is an empty slice.

I'd suggest:

x[:len(x)-d]
over 4 years ago · Santiago Trujillo Denunciar

0

You could use None, which is the default for missing start/stop/step values:

x[:-d or None]
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda