Skip to content

Instantly share code, notes, and snippets.

@tokibito
Created July 19, 2013 10:01
Show Gist options
  • Save tokibito/6038088 to your computer and use it in GitHub Desktop.
Save tokibito/6038088 to your computer and use it in GitHub Desktop.
DATA = [
{'number': 2, 'name': 'b'},
{'number': 1, 'name': 'a'},
{'number': 4, 'name': 'd'},
]
def make_table(lst, key_func=lambda v: v):
table = {}
for value in lst:
key = key_func(value)
table[key] = value
return table
def main():
"""
>>> main()
1, a
2, b
3, None
4, d
5, None
"""
table = make_table(DATA, lambda v: v['number'])
for i in range(1, 6):
value = table.get(i)
print("{}, {}".format(i, value and value['name']))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment