List comprehensions

List comprehensions provide a concise way to create lists from existing lists. Each list comprehension consists of an expression followed by a for clause, then zero or more for or if clauses. The result will be a list resulting from evaluating the expression in the context of the for and if clauses which follow it.

The following are common ways to describe lists (or sets, or tuples, or vectors) in mathematics.

Lists can contain any type of elements, including strings, nested lists and functions. We can even mix different types within a list.
The following works on a list of strings and produces a list of lists.

The following is also an example of nested list comprehension:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The WordPress.com Blog

The latest news on WordPress.com and the WordPress community.

%d bloggers like this: