0
result = [(u'ABC', u'(Choose field)', u'ABCD', u'aa', u'A', u'A_100')]

I'm trying to remove '(Choose field)' from the above list using the following syntax:

result.remove('(Choose field)')
# and  
result.remove("'(Choose field)'")

But both things are not working fine and it ends up with this error

{ValueError}list.remove(x): x not in list
Georgy
  • 12,464
  • 7
  • 65
  • 73
  • 4
    Your list doesn't contain strings. It contains one tuple, which contains strings. However you're populating it, you're doing it wrong. – khelwood Oct 30 '17 at 09:02
  • So may i have to work in How to remove tuple ? – Mohammad Ahmad Shabbir Oct 30 '17 at 09:03
  • If you want this to work, your result list should look like this: `result = [u'ABC', u'(Choose field)', u'ABCD', u'aa', u'A', u'A_100']`. Your list has only one tuple of strings and not different elements of strings. – Vasilis G. Oct 30 '17 at 09:04
  • 1
    You could convert it, e.g. `result = list(result[0])` . Or you could fix whatever's populating the list to do it correctly. – khelwood Oct 30 '17 at 09:04
  • Possible duplicate of [Is there a simple way to delete a list element by value?](https://stackoverflow.com/questions/2793324/is-there-a-simple-way-to-delete-a-list-element-by-value) – sonu_chauhan Oct 30 '17 at 09:06
  • 1
    @sonuchauhan a duplicate of the question title, but it won't solve the issue in this question – roganjosh Oct 30 '17 at 09:07
  • Mr @sonuchauhan Well Title are same but Type and stuff is totally Different – Mohammad Ahmad Shabbir Oct 30 '17 at 09:29

3 Answers3

6

First of all, your list contains tuple which contains string. And tuple doesn't support remove Just convert tuples as list and then use remove

>>> res = list(result[0])
['ABC', '(Choose field)', 'ABCD', 'aa', 'A', 'A_100']
>>> res.remove('(Choose field)')
['ABC', 'ABCD', 'aa', 'A', 'A_100']
akash karothiya
  • 5,736
  • 1
  • 19
  • 29
1

You can convert the tuple inside the list to another list and remove the item from there. This should do the work:-

result = list(result[0])
result.remove(u'(Choose field)')
Abhijeetk431
  • 847
  • 1
  • 8
  • 18
0

If you want to use indexing and specifically want to remove 'a' and 'b' from distinct list: values = ['a', 1, 2, 3, 'b'] - you can do this:

pop_list=['a','b']
[values.pop(values.index(p))  for p in pop_list if p in values]    

above will remove 'a' and 'b' in place - to get:

print(values)   

[1, 2, 3]
Grant Shannon
  • 4,709
  • 1
  • 46
  • 36