0

Say I have a list that looks like this

f = [['person','place','item'],['george','home','phone']] 

how would I search through that list and delete a specific cell using f.remove() I have tried:

f.remove('item') 

which should remove 'item' but instead it returns the error

Exception has occurred: ValueError list.remove(x): x not in list 
8
  • 2
    f[0].remove('item') Commented Jan 2, 2021 at 7:49
  • Do you also want to remove the corresponding element in each of the lists? Commented Jan 2, 2021 at 7:58
  • Also, I suggest that you learn about dictionaries. They are a much better structure when you want to label values with a name. Commented Jan 2, 2021 at 7:58
  • 1
    @SeaverOlson I suggest you check out csv.DictWriter that writes a list of dicts to a CSV file. Commented Jan 2, 2021 at 8:04
  • 1
    Also, the accepted answer is right only if you want to replace the entire first row with None. Commented Jan 2, 2021 at 8:06

5 Answers 5

2

you have list of lists, if you want to remove item from 1st list, you should do the following way:

f[0].remove('item') 

.remove() is inplace operation and doesn't return any value. i.e it will change the list itself, so after above operation your list will be:

print(f) # output [['person', 'place'], ['george', 'home', 'phone']] 

for second list:

f[1].remove('home') 
Sign up to request clarification or add additional context in comments.

1 Comment

list.remove('val') can result in ValueError. The solution for the OP should be complete. Can you use the try:except or if statement to ensure this does not result in an exception situation.
1

You can use list comprehension to do this. You need to put the if statement inside the inner loop.

f = [['person','place','item'],['george','home','phone']] f = [[a for a in x if a != 'item'] for x in f] 

The output of this will be:

[['person', 'place'], ['george', 'home', 'phone']] 

This is equivalent to:

f = [['person','place','item'],['george','home','phone']] new_f = [] for x in f: temp_list = [] for a in x: if a != 'item': temp_list.append(a) new_f.append(temp_list) print (new_f) 

The output of both will be the same.

2 Comments

Thank you I double-checked the answer I decided was correct and it did not work in the case that the list was not empty yet. Thank you for pointing that out
This solution will work irrespective of whether the value exists in the original list or not. For example, if we replace item with apple, it will print the full list as apple is not in the list. With list.remove('apple'), you need to solve for ValueError
0

Have you tried this?

f[0].remove('item') 

Comments

0

You can do it easily using list comprehension

f=[x.remove('item') if 'item' in x else x for x in f] 

1 Comment

x.remove('item') will return a None. So you will NOT get the desired result. you need send all values of x except item
0

In your example f is a two-dimensional list, so all the items in f are also lists, and item is not one of them. Depending on your use case, I can see multiple ways of interpreting "remove":

If you want your resulting list to be [['person','place'],['george','home','phone']], you can use

f[0].remove('item') 

If you want your resulting list to be [['person','place'],['george','home']] (i.e. removing the whole "column" from the 2D list), then you should first find the index you want to remove, and then use del based on the index:

index = f[0].index('item') for row in f: del row[index] 

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.