Official website for Linux User & Developer
FOLLOW US ON:
Feb
12

Another ten essential Python tips

by Kunal Deo

To compliment our ‘ten essential Python tips for beginners’ and ‘ten more essential Python tips’ features, we’ve compiled yet another collection of ten gems to make your Python experience that much more worthwhile…

Python is one of the most popular programming languages in the open source space. Look around and you will find it running everywhere, from various configuration tools to XML parsing. To compliment our ‘ten essential Python tips for beginners‘ and ‘ten more essential Python tips‘ features, we’ve compiled yet another collection of ten gems to make your Python experience that much more worthwhile… Any bets on whether we’ll do another?

Converting any charset to UTF-8
You can use the following function to convert any charset to UTF-8.

data.decode(“input_charset_here”).encode(‘utf-8’)

Removing duplicates from lists
If you want to remove duplicates from a list, just put every element into a dict as a key (for example with ‘none’ as value) and then check dict.keys().

from operator import setitem
def distinct(l):
d = {}
map(setitem, (d,)*len(l), l, [])
return d.keys()

Do-while loops
Since Python has no do-while or do-until loop constructs (yet), you can use the following method to achieve similar results:

while True:
do_something()
if condition():
break

Detecting system platform
To execute platform-specific functions, it is very useful to detect the platform on which the Python interpreter is running. You can use ‘sys.platform’ to find out the current platform.

[Example On Ubuntu Linux]
>>> import sys
>>> sys.platform
‘linux2’

[On Mac OS X Snow Leopard]
>>> import sys
>>> sys.platform
‘darwin’

Pages: 1 2
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.
    • gwb

      The example of removing dups from a list can also be done with set semantics:

      newlist = list(set(oldlist))

      Note that both this approach and the original one will destroy the order of the elements. If you want to preserve order, something like this would do it:

      def unique(list_):
      s = set()
      newl = []
      for x in list_:
      if x not in s:
      new.append(x)
      s.add(x)
      return newl

      You could even be tricksy with something like this:

      listset = set()
      newlist = list(x for x in oldlist if x not in listset and not listset.add(x))

      since set().add() returns None which is boolean False so “not set().add()” will always be true. However, this method involves redundant calls to set().add().

    • Tom K

      I think you want to complEment the other collections of tips, not complIment them.

    • paul

      Removing duplicates from lists can be done a lot easier with set. E.g.:

      >>> l = [1,2,2,3,4,4,5]
      >>> set(l)
      set([1, 2, 3, 4, 5])

    • Rick

      Since white space is significant in python, several of the code examples need to be cleaned up.

    • gwb

      the problem with the white space seems to be a problem of the site’s handling of data. Dunno if it supports pre-formatted tags. Let’s try:

      this
      is
      indented

    • gwb

      nope. didn’t work

    • Lawrence D’Oliveiro

      What do you think your “gc” example is achieving? It’s only referencing the functions, not calling them. I think you want empty parentheses “()” after each function name, to indicate you’re calling them.

    • Lawrence D’Oliveiro

      Removing duplicates: do it in one line using sets, not dictionaries:

      >>> l = (1, 2, 2, 3, 4)
      >>> l
      (1, 2, 2, 3, 4)
      >>> type(l)(set(l))
      (1, 2, 3, 4)
      >>> l = [3, 4, 3, 5, 6]
      >>> l
      [3, 4, 3, 5, 6]
      >>> type(l)(set(l))
      [3, 4, 5, 6]

    • Pingback: Another ten essential Python tips | Linux User | Errors & Geeks

    • anon

      just came across a link to few free python book. very useful for python beginners
      http://cmdlinetips.com/2011/09/free-python-books-online/

    • neo

      this blog will be useful http://www.impythonist.wordpress.com