-2

I have a list that contains some stuff similar to the below entry: [[1, 'Potato', 2, 'Bag'], [2, 'Banana, 1, 'Bunch']]. These elements represent in order the item's UPC, Name, Quantity, and unit type. What I want to do is delete all the items at a certain index to change the list from the above to [[1, 'Potato', 2, 'Bag']]. I tried a couple of methods, but I either used them wrong or they weren't supposed to be used there (I used del, pop, and remove). How do I go about doing this?

11
  • Did you try slicing? Commented Feb 14, 2022 at 20:25
  • How do I do that? Commented Feb 14, 2022 at 20:26
  • 3
    del your_list[1] should have worked here, can you show what you tried and the error you got / how it was different from your expected output? Commented Feb 14, 2022 at 20:26
  • As would your_list.pop(index). Please see How to Ask and the question checklist, and provide a minimal reproducible example Commented Feb 14, 2022 at 20:27
  • [start_index: last_index] to an list. For example [1, 2, 3, 4, 5][:3] will produce [1, 2, 3] Commented Feb 14, 2022 at 20:27

1 Answer 1

1

del works for your example

>>> data = [[1, 'Potato', 2, 'Bag'], [2, 'Banana', 1, 'Bunch']]
>>> del data[1]
>>> data
[[1, 'Potato', 2, 'Bag']]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.