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

Question:

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?

Asked By: Python

||

Answers:

If your list is stored under my_list then this should work.

my_list = my_list[1:-1]
Answered By: Jakob Bowyer

I’m not sure exactly what you want since it is unclear but this should help. You actually only have a single element in that list.

Assuming all your list items are strings with spaces as delimiters, here is how you can remove the first and last group of characters from each string in the list.

>>> L = ['Q 0006 005C 0078 0030 0030 0033 0034 ONE_OF 0002 ']
>>> [' '.join(el.split()[1:-1]) for el in L]
['0006 005C 0078 0030 0030 0033 0034 ONE_OF']
Answered By: jamylak
#initialize 2 lists what will hold your integers
List1 = []
List2 = []

# prompt the user to enter the integers in the list
List1 = eval(input("Enter the list of integers:")

#Iterate over the list to remove the first and last integer
for i in range(len(List1):
    List2 = list1[1:-1]

#print the contents of your new list to verify the results
print(List2) # the output shouldn't contain first and last integer of List1
Answered By: CHARLES S
# prompt the user to enter the integers in the list
List1 = eval(input("Enter the list of integers:")

#Iterate over the list to remove the first and last integer
for i in range(len(List1):
    List2 = list1[1:-1]

#print the contents of your new list to verify the results
print(List2) # the output shouldn't contain first and last integer of List1
Answered By: CHARLES S

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]
Answered By: Giorgos Myrianthous

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.

Answered By: Grismar
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.