tags:

views:

203

answers:

9

I am new to python. 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?

+4  A: 

Easy, you can try this

del a[0][0]
abhiomkar
+2  A: 
a[0].remove(3)

(had to add more text so it was long enough)

Amit
I'm downvoting because you took the easy way out and provided only a code sample and a "more text to hit the limit" statement which helps no one. Put a little more effort into your answers rather than padding them with filler.
Bryan Oakley
@Bryan: what do you say about the accepted answer then?
Amit
@Bryan, downvoting Amit isn't entirely fair, since the answer was edited by others. Check his original... about equivalent to the accepted answer.
Peter Hansen
@Bryan Oh btw, "(had to add more text so it was long enough)" wasn't added by me! It was @Skilldrick
Amit
Thanks @Peter Hansen.
Amit
@Amit: what do I say about the accepted answer? Marginally better than this one. Still a little weak IMO.
Bryan Oakley
@Peter: I didn't downvote Amit, I downvoted this answer. Maybe that's splitting hairs, but that's how I see it. Like I said in my original comment, I think the answer needs a bit more effort.
Bryan Oakley
+2  A: 

Using this:

del a[0][0]

For a better understanding of lists, dictionaries, etc., I suggest you should read DiveIntoPython http://diveintopython.org/ You'll find Chapter 3 very useful.

Alex
A: 

If I understand your question you have list in a list and you want to delete first element from first list. So use:

del a[0][0]
Michał Niklas
+1  A: 

if you don't know where "3" is,

>>> for n,i in enumerate(list):
...   if 3 in i: list[n].remove(3)
...
>>> list
[[4], [5], [6, 7, 8]]
>>>
ghostdog74
A: 

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.

Francesco
+6  A: 

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 :)

Randle Taylor
+1 for teaching the man to fish. (I also reformatted and made it clear that `del` is a statement, not a function.)
Peter Hansen
+1  A: 

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]]
SilentGhost
I upvoted since the answer is useful, but after more than a year of python programming I still don't see the advantage to list comprehensions like this over something with a couple more lines of code that is perhaps a bit less obtuse. ghostdog74's answer is almost instantly recognizable whereas I always have to pause and mentally parse list comprehensions. <shrug>
Bryan Oakley
@Bryan: how is this *obtuse*? ghostdog74's answer is incorrect, as it only removes a single 3 it's also deeply nested, which ghostdog74 is trying to avoid by flattening the remove and making it even less readable or pythonic.
SilentGhost
I say it's obtuse because unless you have used a lot of list comprehensions, you have to mentally parse through what is happening. All developers can pretty much instantly recognize and understand nested loops much faster than they can parse something like "i for i in el". Maybe I'm just an old dog that can't learn new tricks, but I prefer explict loops over one-line list comprehensions.
Bryan Oakley
It's hard to say if ghostdog74's answer is incorrect or not. The original problem says "I want to delete 3". Not *all* 3's. One could interpret it as "I want to remove the first element of the first nested list" or "I want to remove the 3 from the first nested list", or a couple other ways. <shrug>
Bryan Oakley
@Bryan: and ghostdog74's version is doing neither.
SilentGhost
weird. I get the result [[4], [5], [6, 7, 8]] with ghostdog74's version. Whatever. I don't think his is the best solution, but regardless, it doesn't alter the fact I don't see much advantage in list comprehensions.
Bryan Oakley
A: 

two easy way for remove

a[0].pop (0)
del a[0][0]
Mr Jmad