0

Say for example:

list = { [1,2,3],[4,5,6],[3,4],[2,7,8,9] }

is there a way in python to remove a sublist and make sure that other sublists index's remain the same. So for example if i was to remove the sublist [3,4], could i make sure that the index of [2,7,8,9] remains as 3 in this case? if this is possible it would really be helpful! thanks!

Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
  • 2
    Set isn't an ordered data structure, therefore the term `index` is irreverent here. Do you mean 2D list? – Ahsanul Haque Nov 22 '15 at 04:34
  • 1
    First of all `list = { ... }` is **not** a list. And you could simply replace the sublist with a `''`empty character or `None`. – JRodDynamite Nov 22 '15 at 04:34
  • @AhsanulHaque yes 2d list, i want to remove [3,4] in this example without changing the [col][rows] of the other sublists. –  Nov 22 '15 at 04:38
  • 1
    How about deleting the elements in `[3,4]` and keep the empty list like so '[]' – Joe T. Boka Nov 22 '15 at 04:49

3 Answers3

2

Maybe. It depends on how you use the list and just how "listy" the resulting object needs to be.

l = [ [1,2,3],[4,5,6],[3,4],[2,7,8,9] ]

You could replace the sublist with something else like a None. Then your code would have to know to ignore None when it processes the list.

print(l[3])
l[2] = None
print(l[3])

or you could convert the list to a dict and delete the member. You can still index the object but since its now a dict, your code will have to treat it like a dict.

l = dict(enumerate(l))
print l[3]
del l[2]
print l[3]

These tricks will only work in some specialized environments.

tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • so if i put none python will know to ignore it and when its brought up it wont be an error? thx for answering btw i appreciate it. –  Nov 22 '15 at 04:47
  • if i run the 2d list in a loop i get an error, it says object of none type has no length. –  Nov 22 '15 at 04:49
  • Its not that python knows to ignore it... its that your code could perhaps be written to ignore it. That's where the "maybe" comes from. If your code is processing the lists, it may be sufficient to clear the sublist. To remove all items from a list, you can `del somelist[:]`. Since you have a sublist, `del l[2][:]` is it. Other than that, if you bump into problems you might be able to tweek your code or it may mean this hack isn't going to work. – tdelaney Nov 22 '15 at 05:16
1

You can just delete the elements in [3,4] and keep the empty sub-list.

>>> lst = [[1,2,3],[4,5,6],[3,4],[2,7,8,9]]
>>> del lst[2][:]
>>> lst
[[1, 2, 3], [4, 5, 6], [], [2, 7, 8, 9]]

Please note that you should not use list as a variable name as list is a built in function

Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48
0

But you can store the index with the value as a tuple. First make a modified list which has both index and value. Then you are free to remove any of the element.

lst = [[1,2,3],[4,5,6],[3,4],[2,7,8,9]]
modified = list(enumerate(lst))

To explain a bit:

modified=[]

for i,v in enumerate(lst):
    modified.append((i,v))

print modified

Output:

[(0, [1, 2, 3]), (1, [4, 5, 6]), (2, [3, 4]), (3, [2, 7, 8, 9])]
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57