{ "metadata": { "name": "", "signature": "sha256:9e692a2660aaaf23687322cbee6b26045b9869e1a990404162531704da23d6c6" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating Lists From Dictionary Keys And Values\n", "\n", "- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n", "- **Date:** -\n", "- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n", "- **Note:**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a dictionary" ] }, { "cell_type": "code", "collapsed": false, "input": [ "dict = {'county': ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'], \n", " 'year': [2012, 2012, 2013, 2014, 2014], \n", " 'fireReports': [4, 24, 31, 2, 3]}" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a list from the dictionary keys" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Create a list to place the dictionary keys in\n", "dictionaryKeys = []\n", "\n", "# For each key in the dictionary's keys,\n", "for key in dict.keys():\n", " # add the key to dictionaryKeys\n", " dictionaryKeys.append(key)\n", "\n", "# View the dictionaryKeys list\n", "dictionaryKeys" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "['year', 'county', 'fireReports']" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a list from the dictionary values" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Create a list to place the dictionary values in\n", "dictionaryValues = []\n", "\n", "# For each key in the dictionary's Values,\n", "for x in dict.values():\n", " # add the key to dictionaryValues\n", " dictionaryValues.append(x)\n", "\n", "# View the dictionaryValues list\n", "dictionaryValues" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "[[2012, 2012, 2013, 2014, 2014],\n", " ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'],\n", " [4, 24, 31, 2, 3]]" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }