Skip to content

Instantly share code, notes, and snippets.

@bearfrieze
bearfrieze / comprehensions.md
Last active December 23, 2023 22:49
Comprehensions in Python the Jedi way

Comprehensions in Python the Jedi way

by Bjørn Friese

Beautiful is better than ugly. Explicit is better than implicit.

-- The Zen of Python

I frequently deal with collections of things in the programs I write. Collections of droids, jedis, planets, lightsabers, starfighters, etc. When programming in Python, these collections of things are usually represented as lists, sets and dictionaries. Oftentimes, what I want to do with collections is to transform them in various ways. Comprehensions is a powerful syntax for doing just that. I use them extensively, and it's one of the things that keep me coming back to Python. Let me show you a few examples of the incredible usefulness of comprehensions.

@mrdcbrush
mrdcbrush / thisOldiPadiTunesSearch.rb
Last active December 25, 2015 07:19
Searches the itunes store for ios applications that are compatible with the original iPad. Alter the term and rating parameters to filter results.
require 'net/http'
require 'uri'
require 'json'
term = 'calendar'
rating = 4
uri = URI("https://itunes.apple.com/search?term="+ term +"&country=us&media=software&entity=iPadSoftware&limit=50")
resp = Net::HTTP.get_response(uri)
results = JSON.parse(resp.body)