Comprehending Comprehensions in Elixir

While working through Dave Thomas' excellent book on Elixir I got hung up a bit on comprehensions.

Comprehensions are a nice way to loop over an Enumerable do some pattern matching, filtering, and perform a function on the elements.

The problem I was having was with the pattern matching. I had some data in the form of a list of keyword lists.

orders = [  
  [id: 1, name: "Bob", state: :CA],
  [id: 2, name: "Larry", state: :WA]

I thought I could do something like

for [id, name, state] <- orders, state == :CA, do: id  

My misunderstanding was in the true representation of a Keyword list. What each order really looks like is a list of tuples [{:id, 1}, {:name, "Bob"}, {:state, :CA}.

With this knowledge I was able to fix the comprehension and get my desired result.

for [{:id, id}, _, {:state, state}] <- order, state == :CA, do: id  

Went to learn about comprehensions and as a side effect shored up my misunderstanding of keyword lists!