{ "metadata": { "name": "", "signature": "sha256:c1920296974a1149dd163cbcd06a2e9b1e3b444bde7179aa45bb616ff6e9581d" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Iterate An Ifelse Over A List\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 some data" ] }, { "cell_type": "code", "collapsed": false, "input": [ "word_list = ['Egypt',\n", " 'Watching',\n", " 'Eleanor']\n", "\n", "vowels = ['A',\n", " 'E',\n", " 'I',\n", " 'O',\n", " 'U']" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Is valid\n", "Invalid\n", "Is valid\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a for loop" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# for each item in the word_list,\n", "for word in word_list:\n", " # if any word starts with e, where e is vowels,\n", " if any([word.startswith(e) for e in vowels]):\n", " # then print is valid,\n", " print('Is valid')\n", " # if not, \n", " else:\n", " # print invalid\n", " print('Invalid')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Is valid\n", "Invalid\n", "Is valid\n" ] } ], "prompt_number": 12 } ], "metadata": {} } ] }