Python – Throwaway Variables

I’ve recently been using Python in my day job, and it’s a pretty neat language – Industrial Light and Magic, Google, Facebook, etc. all use it. It is one of the most versatile languages out there, along with Java.

A particular trick that I discovered recently, is throwaway variables. They are very useful for extracting values. They are denoted using an underscore and comma _, to tell the interpreter to ignore the next set of alphanumeric symbols.

Here’s an example:

names = "Peter, Daniel, Sarah, Tom, Danielle, Amy, Sebastien"
_, _, a, _, b, c, _ = names.split(",")
print "{}, {}, {}".format(a, b, c)

Output: Sarah, Danielle, Amy

Pretty neat!

This entry was posted in Python and tagged , . Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>