I have created a list
a = [[3, 4], [5], [6, 7, 8]]
I want to delete 3 from this list. What is the command for this?
lots of possible ways
>>> mylist = [[3,4],[5],[6,7,8]]
>>> mylist[0] = [4]
>>> mylist
[[4], [5], [6, 7, 8]]
>>> mylist = [[3,4],[5],[6,7,8]]
>>> del mylist[0][0]
>>> mylist
[[4], [5], [6, 7, 8]]
>>> mylist = [[3,4],[5],[6,7,8]]
>>> mylist[0].remove(3)
>>> mylist
[[4], [5], [6, 7, 8]]
Take your pick :)
del is a statement, not a function.)Assuming, you want to delete all 3s from a list of lists:
>>> lst = [[3,4],[5],[6,7,8]]
>>> [[i for i in el if i != 3] for el in lst]
[[4], [5], [6, 7, 8]]
Using this:
del a[0][0]
For a better understanding of lists, dictionaries, etc., I suggest you should read Dive Into Python You'll find Chapter 3 very useful.
First of all, be careful because you are shadowing the built in name "list". This
a_list = [[3,4],[5],[6,7,3, 3, 8]]
def clear_list_from_item(a_list, item):
try:
while True: a_list.remove(item)
except ValueError:
return a_list
a_list = [clear_list_from_item(x, 3) for x in a_list]
This will modify your original list in place.
listis a bad name for your object since it shadows a built-in.