In Python, how to slice a list to get the first element, and all elements except the last? -


given:

a = [1, 2, 3] b = [4,5] 

how get:

[1, 2] [4] 

in above example, know works:

a[:-1] b[:-1] 

however, when:

c = [1] c[:-1] 

the result empty list.

you want following code fragment:

c[:1] + c[1:-1] 

for example:

>>> c = [1] >>> c[:1] + c[1:-1] [1] 

with c[:1] list consisting of first element (if exists, otherwise empty list), , c[1:-1] second until end except last. if there isn't second element c[1:-1] return empty list.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -