• Removing multiple elements from a list in Python

    Removing multiple elements from a list in Python

    Let’s say you need to filter a Python list and remove all elements that match a given criteria. If you wanted to remove a single element, you could just use ‘del list[i]’ (example). But if you wanted to remove multiple elements this might be a problem since you would be modifying and iterating over the list at the same time (keeping track of list indexes can become very confusing, very fast).

    A simple solution to this problem would be to keep record of all list elements (indexes) that need to be removed, and to remove tham afterwards. Also, one other thing to be wary about is that the removal process should be done in reverse because otherwise you would shift all the elements to the ‘left’ every time you removed an element from a list and you would (again) have to keep track of list indexes.

    A simple function example:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    matchingFilter = ['criteria1', 'criteria2']

    def criteriaFiltering(aList, matchingFilter):
       deletionIndexes = []
       i = 0

       for listLine in aList:
          for match in matchingFilter:
             if match in str(listLine):
                continue             
             else:
                deletionIndexes.append(i)
                break
          i += 1

       for number in reversed(deletionIndexes):
          del listLines[number]

       return aList

    This might not be the fastest ‘algorithm’ to do the job, but it works well enough for me..

  • How to remove ‘lastsaid’ history and chat logs from Pidgin

    Turning on History plugin in Pidgin will insert the last conversation into your current conversation. All this lastsaid entries can be found somewhere inside of blist.xml file which is located in your Pidgin conf directory (/home/username/.purple directory in GNU/Linux (not sure about where it is in MS Windows though)).

    This is convinient if you want to view what was written before you accidentialy closed the chat window or if you forgot what were you last talking about with someone and if you want that information quickly. But, it is not convinient at all if you want to wipe it along with chat logs since lastsaid entries are at a different location and are not the only entries that can be found in blist.xml so you can’t just remove that file either. If you use a couple of protocols and have more than just a few buddies, removing lastsaid lines from blist.xml manually would probably prove to be pain in the ass.

    So, what every curious programmer does in case he doesn’t have a feature he wants/needs? – He just makes it. :D Since I started playing with Python, I decided to create a small py3k script I could use every once in a while to completely wipe both lastsaid history and chat logs from Pidgin.

    What you need to do to make this script work is – you need to put it into your Pidgin conf directory (just put it into a file called something.py) and after that just run the script (you need to have python installed on your machine to run this script).

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #! /usr/bin/env python3
    import xml.etree.ElementTree as etree
    import shutil
    import os

    # deletes 'lastsaid' blist.xml entries
    if os.path.exists("blist.xml"):
       tree = etree.parse("blist.xml")

       buddies = tree.findall(".//buddy")      
       for buddy in buddies:
          nodes = buddy.findall("setting")    
          for node in nodes:
             if node.attrib == {"type": "string","name": "lastsaid"}:
                buddy.remove(node)

    tree.write("blist.xml")


    # deletes chat log files if they exist
    if os.path.exists("logs"):
       shutil.rmtree("logs")

    I hope this helps. Also, if you have any questions/problems with the script, feel free to drop a comment..

  • Can't remove firefox tags

    Dunno why but in firefox 3 after adding a tag to a bookmark, later on, I haven’t been able to delete it no matter what I tried… hopefully there IS a solution.

    1) download firefox extension: SQLLite
    2) run it.
    3) at the top right of screen, choose places.sqllite, click go.
    4) select Tables -> moz_bookmarks
    5) click browse&search tab, and then click on search
    6) under “6. title=” box enter “nameOfYourTag”
    7) right click -> delete.

    It’s funny mozzila dedevelopers didn’t integrate the option for this…
    Thanks to people who found a way to fix tags.

Back to top