Python Forum
How to use Del with list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use Del with list
#1
I am doing a basic practice exercise and am having issues resolving the Chop function. The goal is to use a function called Chop that removes the first and last item in the list. Since I do not know what index the last item is I used the len function. Where I am having issues is deleting the first and last index of the list. Here is what I have so far, https://hastebin.com/ruwinuweye.py
Reply
#2
You can use slice:
def chop(lname):
    return lname[1:-1]
Reply
#3
Thank you for your response. I was considering using a slice as you mentioned but was hoping there was an alternative using the del function to permanently remove the item from the list. Is it possible to pass a variable to the del function's index? The ideal situation is if I could use len/range to identify the total items in the list after the loop iteration searches through all the items. Then set that total number to a variable that can be inserted as index for deletion.

https://hastebin.com/qaqixemaza.py
Reply
#4
We're not going to look at external links. If you have code, post it here, between Python tags as described in the BBCode link in my signature below.

You don't need len to find the last index of a list. -1 is always the index of the last item in the list, no matter how long the list is, just as 0 is always the index of the first item of the list.

del target_list[0]
del target_list[-1]
And, yes, you can use a variable.

last_item = -1
del target_list[last_item]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
This is what I have so far. Numbers on the left are line numbers from hastebin. If I use the del function on one index it works fine with a number index. As soon as I try to del with a variable or one index and variable I receive an error. Currently, I am seeing my_list_len as an index value of 5. I have reduced that by 1 so it corresponds with index values. So I am not sure why my index is out of range.

def chop(my_list):
7 count = 0
8 for elements in my_list:
9 my_list_len = len(my_list)
10 count = count + 1
11 last = my_list[my_list_len - 1]
12 # del my_list[1, last] If I could delete both at once would be much easier.
13 # deleting one at at item throws off index value of the second deletion
S> 14 del my_list[last] #IndexError: list assignment index out of range
15 print(my_list, count)
16
S> 17 t = ['item1', 'item2', 'item3', 'item4', 'item5']
18 chop(t)
Reply
#6
Use python tags when posting code. See the BBCode link in my signature below for instructions. We can't see the indentation without the tags. And I really want to see the indentation, because I have no idea why you have a for loop in there. And if you are getting an error, post the full text of the error.

My guess is the second deletion doesn't work because the first deletion changes the length of the list. Which is why we keep telling you to use -1 for the index of the last item of the list.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
 def chop(my_list):
    7     count = 0
    8     for elements in my_list:
    9         my_list_len = len(my_list)
   10         count = count + 1
   11     last = my_list[my_list_len - 1]
S> 12    # del my_list[1, last] If I could delete both at once would be much easier.
S> 13    # deleting one at at item throws off index value of the second deletion
S> 14     del my_list[last]  #IndexError: list assignment index out of range
   15     print(my_list, count)
   16
S> 17 t = ['item1', 'item2', 'item3', 'item4', 'item5']
   18 chop(t)
Reply
#8
Now eliminate your line numbers and 'S>', whatever that is and you are good
Reply
#9
The loop is useless. Why calculate the length repeatedly? It's the same answer every time though the loop. You get count, but you just seem to be using that to check len(my_list).

The reason you are getting an IndexError is that my_last is the last item of the list, not the index of the last item of the list. You are then using it as the index of the last item of the list, which it is not, when you delete. When you use del seq[x], x needs to be an index of the list (a position within the list) not something that is in the list.

You are really making this far more complicated than it needs to be. The code Larz60+ posted is all you need. If you insist on using del, the code I posted is all you need. There is really no difference in using slicing vs. del, except slicing is simpler. Simpler is good.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
(Sep-20-2018, 08:24 AM)Larz60+ Wrote: Now eliminate your line numbers and 'S>', whatever that is and you are good

The 'S>' and line numbers are a result of copying and pasting the code from the IDE. Line 14 throws off the first error in the comments.

(Sep-20-2018, 02:11 PM)ichabod801 Wrote: The loop is useless. Why calculate the length repeatedly? It's the same answer every time though the loop. You get count, but you just seem to be using that to check len(my_list).

The reason you are getting an IndexError is that my_last is the last item of the list, not the index of the last item of the list. You are then using it as the index of the last item of the list, which it is not, when you delete. When you use del seq[x], x needs to be an index of the list (a position within the list) not something that is in the list.

You are really making this far more complicated than it needs to be. The code Larz60+ posted is all you need. If you insist on using del, the code I posted is all you need. There is really no difference in using slicing vs. del, except slicing is simpler. Simpler is good.

The intent of the loop was to count through all the items of the list as a way to double check len. As you mentioned its not needed to accomplish the main goal. I was looking to remove the two items in the list through two specific processes. First is to identify the total number of items in the list using len as a way to calculate the index of the last list item. Then set that to a variable which I would use to represent the adjusted length which should have a value for the index of the last list item.

Then, I was hoping to remove the first and last item of the list using the del function. With the del function I was wondering if it was possible to delete two items at once so that it does not affect the index value of the second deleted item. Using for example del[0, last]. If del does not allow for two indexes at once, then how would I use delete twice adjusting for the index value changes upon deletion?

def chop(my_list):
    7     count = 0
    8     my_list_len = len(my_list)
    9     for elements in my_list:
   10         my_list_len = len(my_list)
   11         count = count + 1
   12     last = int(my_list_len) - 1
S> 13    # last = my_list[my_list_len - 1]
S> 14    # del my_list[1, last] If I could delete both at once would be much easier.
S> 15    # deleting one at at item throws off index value of the second deletion
S> 16     del my_list[last] #IndexError: list assignment index out of range
   17     del my_list[0]
   18     print(my_list, count)
   19
S> 20 t = ['item1', 'item2', 'item3', 'item4', 'item5']
   21 chop(t)
Those modifications produced the correct output. Although when I try to use, del my_list[1, last], I receive this error: TypeError: list indices must be integers or slices, not tuple.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020