简体   繁体   中英

How to remove the first and last item in a list?

I have the List

['Q 0006 005C 0078 0030 0030 0033 0034 ONE_OF 0002 '] 

How do I remove the first element, Q and 0002 , the last element?

I have the List

['Q 0006 005C 0078 0030 0030 0033 0034 ONE_OF 0002 '] 

How do I remove the first element, Q and 0002 , the last element?

I have the List

['Q 0006 005C 0078 0030 0030 0033 0034 ONE_OF 0002 '] 

How do I remove the first element, Q and 0002 , the last element?

Another question was redirected here, asking what this line does, given that res is a list:

c, *res, c = res

This uses multiple assignment and extended iterable unpacking. In Python you can do something like this:

a, b, c = 1, 2, 3

To assign 1 to a , etc. - this is multiple assignment, assigning multiple values in a single assignment statement.

If the right side consists of an iterable, Python will 'unpack' the iterable to assign the parts. For example:

t = (1, 2, 3)  # a tuple
a, b, c = t  # after this, a == 1, b == 2, c == 3

However, in the given example (with c and res ), the iterable on the right doesn't have to have the same number of items as the number of targets on the left.

This is where "extended iterable unpacking" comes in. For the documentation : "allowing to specify a “catch-all” name which will be assigned a list of all items not assigned to a “regular” name."

So, in c, *res, c = res , the first element of res is assigned to c , the last element is assigned to c , but everything else from the unpacked iterable (the list in this case) is assigned to *res , which will make res a list again.

Note the following:

res = (1, 2, 3, 4)
c, *res, c = res

Here, you may expect res to end up as (2, 3) - but that's not the case, after running, res will be [2, 3] , a list . The assignment does not transfer the type, only the values. And the extended iterable unpacking results in res being a list after the assignment, even though the values came from a tuple.

Finally, note that nicer syntax might be:

res = [1, 2, 3, 4]
_, *res, _ = res

The use of _ makes it clear that these values are to be discarded. And it removes questions about what the value of c will be after c, *res, c = res . It's 4 , because the assignments are evaluated left to right, but it's better to be clear.

I have the List

['Q 0006 005C 0078 0030 0030 0033 0034 ONE_OF 0002 '] 

How do I remove the first element, Q and 0002 , the last element?

Another (quite Pythonic) way to do it is using the extended sequence unpacking feature:

my_list = _ , *my_list, _

Example

>>> my_list =  [1, 2, 3, 4, 5]
>>> _, *my_list, _ = my_list
>>> my_list
[2, 3, 4]

I have the List

['Q 0006 005C 0078 0030 0030 0033 0034 ONE_OF 0002 '] 

How do I remove the first element, Q and 0002 , the last element?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM