Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove all list elements starting with a hash

Tags:

python

i have a list with elements, some of which start with "#". how can i remove those elements? I tried:

content = [x for x in content[][0] if x != "#"]

but:

content[][0]

seems to be not valid. What is the best way to do this?

like image 602
Jack S. Avatar asked Aug 14 '11 18:08

Jack S.


People also ask

How do you remove all elements of a list from another list in Python?

How to Remove an Element from a List Using the remove() Method in Python. To remove an element from a list using the remove() method, specify the value of that element and pass it as an argument to the method. remove() will search the list to find it and remove it.

How do I remove indexes from a list?

You can use the pop() method to remove specific elements of a list. pop() method takes the index value as a parameter and removes the element at the specified index. Therefore, a[2] contains 3 and pop() removes and returns the same as output. You can also use negative index values.

How do I remove a list from a list in Python?

Method #3 : Using remove() One can iterate for all the elements of the removed list and remove those elements from the original list.


1 Answers

content = [x for x in content if not x.startswith('#')]
like image 195
Peter Collingridge Avatar answered Oct 12 '22 23:10

Peter Collingridge