-2

So I have a list from which i want to remove something like a sub list. I don't know how to call it exactly so i couldn't find any results searching for an answer.

list = [[0, 1, 0, 0], [0, 1, 1, 1], [1, 1, 0, 0], [0, 0, 0, 0]]

I just want to remove one of those 4 brackets, for example the first one. Should i use "remove" or "delete" or how do i do that?

list = [[0, 1, 1, 1], [1, 1, 0, 0], [0, 0, 0, 0]]
Amir
  • 1,885
  • 3
  • 19
  • 36
user8385652
  • 99
  • 2
  • 9

2 Answers2

0

If you know the index which you want to remove i:

del l[i]
# Or l.pop(i)

If you know the value you want to remove val:

l.remove(val)

P.S: Don't use list as the name of a variable. It's a name of a builtin class

rdas
  • 20,604
  • 6
  • 33
  • 46
  • Thank you very much! Sorry i can't give upvotes because i dont have enough rep. – user8385652 May 09 '19 at 19:11
  • Please note that remove(value) removes the first occurance of value the method encounters. So if the list contains 'xyz' three times, only the first 'xyz' gets removed. – TimB May 09 '19 at 19:17
0

You can del it by its index. For example to delete the first one, you can do:

my_list = [[0, 1, 0, 0], [0, 1, 1, 1], [1, 1, 0, 0], [0, 0, 0, 0]]
del my_list[0]
print(my_list)

# output:
[[0, 1, 1, 1], [1, 1, 0, 0], [0, 0, 0, 0]]
Amir
  • 1,885
  • 3
  • 19
  • 36