Learn python list comprehensions by example

List comprehension is a beautiful way to simplify code. According to the python documentation, “list comprehensions provide a concise way to create lists.” In this guide, I’ll walk through a few different examples of how you can use list comprehensions.

Tools:

  • 1 ea python

1List of squares

If you wanted to create a list of squares for the numbers between 1 and 10 you could do the following: squares = [] for x in range(10): squares.append(x**2) This is an easy example, but there is a much more concise way to write this using list comprehensions. squares = [x**2 for x in range(10)] The basic list comprehension is composed of square brackets surrounding an expression followed by a for statement. List comprehensions always return a list.

2List of numbers divisible by 3

Normally you might try: numbers = [] for x in range(100): if x % 3 == 0: numbers.append(x) You can include an if statement in the list comprehension to conditionally include items. To create a list of numbers between 0 and 100 that are divisible by three, here is a way using a list comprehension: numbers = [x for x in range(100) if x % 3 == 0]

3Finding primes

It would take quite a few lines of code to accomplish this normally. noprimes = [] for i in range(2, 8): for j in range(i*2, 50, i): noprimes.append(j) primes = [] for x in range(2, 50): if x not in noprimes: primes.append(x) However, you can simplify this by using two list comprehensions. noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)] primes = [x for x in range(2, 50) if x not in noprimes] The first line uses multiple for loops within one list comprehension. The first for loop is the outer loop, and the second for loop is the inner loop. To find primes, we are first finding a list of non prime numbers. This list is generated by finding multiples of numbers 2-7. Then we loop through a range of numbers and check to see if each number is in the list of non primes. Edit: as pointed out by shoyer on reddit, using a set for finding noprimes is much more efficient. Since noprimes should contain only unique values and we frequently have to check for the existence of a value, we should be using a set. Set comprehension has similar syntax to list comprehension so we can use the following: noprimes = set(j for i in range(2, 8) for j in range(i*2, 50, i)) primes = [x for x in range(2, 50) if x not in noprimes]

4Flatten a list of lists

Suppose you had a list of lists or a matrix, matrix = [[0,1,2,3], [4,5,6,7], [8,9,10,11]] and you want to flatten it into a single list. You could do so like this: flattened = [] for row in matrix: for i in row: flattened.append(i) And using list comprehension: flattened = [i for row in matrix for i in row] This uses two for loops to iterate through the entire matrix. The outer (first) for loop iterates through the row, and the inner (second) for loop iterates through each item i in the row.

5Simulate a series of coin tosses

Suppose you want to simulate a series of coin tosses where 0 is heads and 1 is tails. You could do this: from random import random results = [] for x in range(10): results.append(int(round(random()))) Or use a list comprehension to make it more concise: from random import random results = [int(round(random())) for x in range(10)] This uses range to loop 10 times. Each time we round the output of random(). Since random() returns a float between 0 and 1, rounding the output will return either 0 or 1. Round() returns a float so we convert it to an integer using int() and add that value to the list.

6Remove vowels from a sentence

Suppose you have a sentence, sentence = 'Your mother was a hamster' and you want to remove all of the vowels. We can do this easily in a few lines: vowels = 'aeiou' non_list = [] for l in sentence: if not l in vowels: non_list.append(l) nonvowels = ''.join(non_list) Or you can clean simplify using list comprehension: vowels = 'aeiou' nonvowels = ''.join([l for l in sentence if not l in vowels]) This example uses a list comprehension to create a list of letters from sentence that are not vowels. Then we pass the resulting list to join() to convert it to a string. Edit: As noted by iamadogwhatisthis on reddit, this example doesn’t require a list comprehension. A generator comprehension is more appropriate: vowels = 'aeiou' nonvowels = ''.join(l for l in sentence if not l in vowels) Notice the missing square brackets. This is because join takes any iterable data to include lists or genetators. This syntax without square brackets uses generator comprehension. It produces the same result, but rather than packing all of the items into a list first it yields them as we iterate through. This prevents us from having to store the entire list into memory, and is more efficient for larger data.

7Get a list of txt files in a directory

The following code will iterate through the files in my_dir directory and append each one that has a txt extension. import os files = [] for f in os.listdir('./my_dir'): if f.endswith('.txt'): files.append(f) This can be simplified with a list comprehension as well: import os files = [f for f in os.listdir('./my_dir') if f.endswith('.txt')] Or you can get a list of the relative paths: import os files = [os.path.join('./my_dir', f) for f in os.listdir('./my_dir') if f.endswith('.txt')] Courtesy of rasbt on reddit.

8Read a csv into a listed dictionary

It’s a frequent requirement to read in data from a csv file and process it. One of the most useful ways to process csv data is to turn it into a list of dictionaries. import csv data = [] for x in csv.DictReader(open('file.csv', 'rU')): data.append(x) You can quickly do this with list comprehension: import csv data = [ x for x in csv.DictReader(open('file.csv', 'rU'))] The DictReader class will automatically use of the first row of the csv file as the dictionary key names. This DictReader class returns an object that will iterate over the lines of the csv file. The file object is created by the open() function. We give open() two parameters – the name of the csv file first and the mode second. In this case, ‘rU’ means two things. As usual ‘r’ means to open the file in read mode. ‘U’ signifies that we will accept universal newlines – ‘n’, ‘r’, and ‘rn’. Courtesy of blacwidonsfw on reddit.


Now learn about:


Discuss this guide

var disqus_shortname = ‘howchootest’; (function() { var dsq = document.createElement(‘script’); dsq.type = ‘text/javascript’; dsq.async = true; dsq.src = ‘//’ + disqus_shortname + ‘.disqus.com/embed.js’; (document.getElementsByTagName(‘head’)[0] || document.getElementsByTagName(‘body’)[0]).appendChild(dsq); })();

Tools:

  • 1 ea python

pi

Find new interests

howchoo guides are organized by interest (e.g. h/jeep). By subscribing to interests, you can learn about and keep track of things that interest you.

osx

OS X is a series of Unix-based operating systems developed by Apple, Inc.[..]
37 guides Explore

mac

35 guides Explore
Apple Computers is an American computer and consumer electronics company [..]
35 guides Explore
All things web development. [..]
26 guides Explore
21 guides Explore
Code is poetry — one line at a time.[..]
12 guides Explore
11 guides Explore
11 guides Explore
9 guides Explore

www

8 guides Explore
7 guides Explore

ios

iOS is the mobile operating system used by the iPhone, iPad, and iPod Tou[..]
7 guides Explore
7 guides Explore
“Any sufficiently advanced technology is indistinguishable from magic.” -[..]
7 guides Explore

vim

7 guides Explore
6 guides Explore

php

The ultimate PHP resource for beginners and experts alike. [..]
5 guides Explore
5 guides Explore
4 guides Explore
4 guides Explore
Produced by Honda between 2003-2011, the Honda Element is a versatile and[..]
4 guides Explore
Hypertext Markup Language (HTML) is a standardized coding language that i[..]
4 guides Explore
4 guides Explore
4 guides Explore
Chrome is a popular web browser created by Google. As of October 2013, Ch[..]
4 guides Explore

pi

4 guides Explore
3 guides Explore

git

3 guides Explore
3 guides Explore
3 guides Explore
3 guides Explore

css

3 guides Explore
3 guides Explore
3 guides Explore
3 guides Explore
3 guides Explore
3 guides Explore
3 guides Explore
3 guides Explore
3 guides Explore
3 guides Explore
3 guides Explore
3 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore

oil

2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
Unveiled at E3 in 2005, the Xbox 360 is Microsoft’s second major gaming c[..]
2 guides Explore
2 guides Explore
iOS 7 is the latest version of the iPhone operating system. It features a[..]
2 guides Explore
ModX is a powerful and customizable open source Content Management System[..]
2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
2 guides Explore
1 guide Explore

ftp

1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore

ip

1 guide Explore
1 guide Explore

vlc

VLC Player is a popular video player for Mac, Windows and Linux that supp[..]
1 guide Explore
1 guide Explore
“They who can give up essential liberty to obtain a little temporary safe[..]
1 guide Explore
Sublime Text is an awesome source code and text editor for Mac, Windows a[..]
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore

s2k

1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore

dba

1 guide Explore
1 guide Explore
1 guide Explore
Also known as “rare earth magnets”, neodymium magnets are incredibly powe[..]
1 guide Explore
Pest control can be traced back to the earliest instances of agriculture.[..]
1 guide Explore
Raccoons are a nocturnal mid-sized mammal native to North America. Raccoo[..]
1 guide Explore
Adium is a popular application for OS X that serves as an alternative to [..]
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
The latest CSS version, CSS3 adds support for media queries, namespaces, [..]
1 guide Explore

llc

A Limited Liability Company (LLC) is a great way to protect your assets w[..]
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore

diy

1 guide Explore

att

1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore

tsa

1 guide Explore
1 guide Explore
1 guide Explore

vin

1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore

irc

1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore
1 guide Explore

pc

1 guide Explore
1 guide Explore
1 guide Explore

Make your own vinyl record clock

If you’re a music enthusiast (or know somebody who is and expects a gift from you), nothing beats a vinyl record clock. I’m a huge fan of simplicity and this clock takes the cake. Note: I chose to purchase a clock movement kit since it was inexpensive and I didn’t have any old clocks lying around to cannibalize parts from.

Tools:

  • 1 ea Drill bit – 5/16
  • 1 ea drill

Materials:

1Choose a record

There’s no point destroying a perfectly good record (let alone one you like), so I recommend choosing one that’s no longer playable. I chose a “Hit Explosion” record that had a pretty big gouge on one side.

Choose a record

2Expand the record’s hole

I chose a 1/4″ clock movement kit because most vinyl record holes are approximately 1/4″ in diameter. This size clock kit includes a clock movement that has a 1/4″ spindle. Using your drill and a 5/16″ drill bit, widen the record’s center hole so that the spindle will more easily fit into the record.

Expand the record's hole

3Attach the clock movement to the record

Push (or screw) the clock movement’s spindle through the record’s center hole. Using the supplied hardware (normally a nut and washer), secure the clock movement to the record. You can tighten the nut with your fingers — using a wrench is unnecessary and might crack the record.

Attach the clock movement to the record

4Paint the clock hands (optional)

Most clock movement kits come with black hands and, since most vinyl records are black, you might want to paint them. I painted my clock hands orange to match the label of the record I’m using. If you don’t feel like breaking out the spray paint, you can order a clock movement with silver or gold hands (though I’ve found the 1/4″ kits to be hard to find in these colors).

Note:

Be sure to use primer spray paint prior to painting your clock hands (which I neglected to do). This will make the color more vibrant Plus, the primer will help to make the paint adhere to the clock hands.

5Attach the clock hands

Clock movement kits vary — I recommend checking the instructions that came with your kit to ensure that you don’t break the very fragile tiny hands.

Attach the clock hands

6Hang up your clock

The clock movement kit I ordered (which I’ve linked to at the top of this guide) has an integrated hanger. This is super useful and saves a ton of time. Now you just need to hammer a nail into the wall and voila! You’re finished!

Hang up your clock

Now learn about:


Discuss this guide

var disqus_shortname = ‘howchootest’; (function() { var dsq = document.createElement(‘script’); dsq.type = ‘text/javascript’; dsq.async = true; dsq.src = ‘//’ + disqus_shortname + ‘.disqus.com/embed.js’; (document.getElementsByTagName(‘head’)[0] || document.getElementsByTagName(‘body’)[0]).appendChild(dsq); })();

Tools:

  • 1 ea Drill bit – 5/16
  • 1 ea drill

Materials:

Install multiple fonts at once in Mac OS X

If you have several fonts to install, why install each one manually? You can batch install a bunch of fonts at once.

Tools:

  • 1 ea Finder
  • 1 ea Mac OS X

1Locate your fonts in Finder

Open a new Finder window and locate the fonts you’d like to install.

Locate your fonts in Finder

2Open Font Book

Navigate to Applications > Font Book (or open it using Spotlight).

Open Font Book

3Batch install the fonts

In Finder, highlight all the fonts you’d like to install and drag them into the Font Book window. You’re done!

Batch install the fonts

Note:

Note: if the fonts you’re trying to install are corrupt or weren’t designed properly, you may see an error message. If you’d like to continue with installation anyways, you can check the associated checkbox to continue with installation.


Now learn about:


Discuss this guide

var disqus_shortname = ‘howchootest’; (function() { var dsq = document.createElement(‘script’); dsq.type = ‘text/javascript’; dsq.async = true; dsq.src = ‘//’ + disqus_shortname + ‘.disqus.com/embed.js’; (document.getElementsByTagName(‘head’)[0] || document.getElementsByTagName(‘body’)[0]).appendChild(dsq); })();

Tools:

  • 1 ea Finder
  • 1 ea Mac OS X

recipes

+ Subscribe

recipes

+ Subscribe

Understanding JavaScript hoisting

In short, hoisting is when JavaScript moves variable and function declarations to the top of their scope before any code is executed. Anyone who has some experience with JavaScript has surely seen the effects of hoisting even if by accident. In my case, I’ve learned to expect the outcome of hoisting without truly understanding it. In this guide, I will aim to cover JavaScript hoisting with examples and explanations.

1Declarations vs. Expressions

At some point, you may have been wondered about the difference between this:
var hello = function() {
alert('hello');
};

and
function hello() {
alert('hello');
}

In order to understand JavaScript hoisting you must understand the difference between the first, a function expression, and the second, a function declaration.

The most important difference is that function declarations (and declarations in general) are processed before any code is executed.

That means that declaring a function:
function hello() {
alert('hello');
}

or a variable:
var x = 1;

make these names ‘hello’ and ‘x’ available in their scope no matter where they are declared.

Because these declarations are processed before any code is executed, declaring them at the bottom of a scope is the same is declaring it at the top. In fact, hoisting literally moves these declarations to the top of the scope, and we’ll cover that in more detail later.

Inversely, expressions are not hoisted and they are processed when the code is executed, not before. So you can see the following will throw an error:
x();

var x = function() {
alert('I am x');
}

whereas this will work as expected:
x();

function x() {
alert('I am x');
}

Before moving on, you should be able to identify a function declaration and a variable declaration, and you should understand that declarations are processed before any code is executed.

2Declarations vs. Initializations

Now you must understand the difference between a declaration and an initialization. JavaScript hoisting applies to declarations NOT initializations.

So the following:
var x = 7;

is both a declaration and an initialization.

The variable ‘x’ is declared like this:
var x

and initialized like this:
x = 7

Understand that declarations alone are hoisted, not initializations. So according to our definition, the declaration var x is processed before any code is executed, while the initialization x = 7 is executed where it is initialized. However, with a function declaration the entire function is processed in advance of any code execution. You will see more examples of these later.

3Basic hoisting examples

When we say that a declaration is processed before any code is executed, that means they are essentially hoisted to the top of the scope.

So in the following:
hello();
function hello() {
alert('hello');
}

the hello function is hoisted to the top of the scope and processed like this:
function hello() {
alert('hello');
}
hello();

And in this:
if (x) {
alert(x);
} else {
alert('not initialized yet');
}
var x = 7;

the variable declaration x is hoisted to the top, the initialization remains in place, and it is processed like this:
var x;
if (x) {
alert(x);
} else {
alert('not initialized yet');
}
x = 7;

It won’t throw an error because the declaration of x has been hoisted to the top, but it will alert “not initialized yet” because x is initialized after.

4Advanced hoisting examples

If you understand hoisting then you should be able to predict the following outcomes:
alert(foo());

function foo() {
function bar() {
return 10;
}
return bar();
function bar() {
return 20;
}
}

if you said 20 you are correct. The previous code would be processed like this:
function foo() {
function bar() {
return 10;
}
function bar() {
return 20;
}
return bar();
}
alert(bar());

After hoisting has taken place, you can more easily see that the function foo would return 20.

How about this one:
var x = 7;
var add = function() {
return x + y;
}
alert(add());
var y = 8;

If you answered “NaN” (not a number) then you are correct. This is because the function add is trying to add the variable x with the variable y which hasn’t yet been initialized. Even though it’s been declared, it hasn’t yet been initialized.
The previous code would be processed like this:
var x = 7;
var add;
var y;
add = function() {
return x + y;
}
alert(add());
y = 8;

You can see that by the time add is actually called, y has no value so it cannot be added to x which was initialized with a value of 7.

Here’s one more:
alert(words());
function words() {
function getWords() {
return "i am a function declaration";
}
var getWords = function() {
return "i am a function expression";
}
return getWords();
}

By now you can probably predict that this program will alert “i am a function expression”. After hoisting this will be processed like so:
function words() {
function getWords() {
return "i am a function declaration";
}
var getWords;
getWords = function() {
return "i am a function expression";
}
return getWords();
}
alert(words());

Not much changes in the order, but you can see that the getWords function variable name overwrites the getWords function declaration when the code is actually executed.

5What does this mean for you?

There are a few things you can take away from this. If you understand hoisting, you should able to prevent hoisting related confusion for yourself or anyone else reading your code in the future. One way to do this is to write your code with hoisting in mind. Make sure to declare your variables and functions at the top of the scope. Since hoisting will essentially do this for you at run-time, you will avoid confusion if you write your code in the order that it will be processed. With this you will be able to plainly see if a variable used in a function hasn’t yet been initialized or if the value changes before the function gets called.

Next, know when to use function declarations as opposed to function expressions. In some cases, it doesn’t really matter, but you should use them intentionally. Understand that function definitions are created before the code is ever executed and a function expression is created in place when the code is executed.

It is generally recommended to use function declarations by default and use function expressions when there is a need.

One case where you would want to use a function expression is when you are creating a function conditionally. Take a look at the following example:
if (true) {
function foo() {
return 'true';
}
} else {
function foo() {
return 'false';
}
}
alert(foo());

Different browsers handle this case differently, but chances are your browser will alert ‘false’! After learning about JavaScript hoisting, this probably makes sense. In this case, you should use function expressions:
var foo;
if (true) {
foo = function() {
return 'true';
};
} else {
foo = function() {
return 'false';
};
}
alert(foo());

Declaring foo at the top of the scope will ensure that this variable remains local. Using function expressions will ensure that the variable foo is initialized when the code is executed rather than being hoisted before.

If you’ve made it this far in the guide, I’m interested to know if you have any other concerns related to hoisting and if you have any other guidelines for when to use function declarations vs. expressions. If so, leave a comment below!


Also in this interest


Discuss this guide

var disqus_shortname = ‘howchootest’; (function() { var dsq = document.createElement(‘script’); dsq.type = ‘text/javascript’; dsq.async = true; dsq.src = ‘//’ + disqus_shortname + ‘.disqus.com/embed.js’; (document.getElementsByTagName(‘head’)[0] || document.getElementsByTagName(‘body’)[0]).appendChild(dsq); })();