Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "List ,[-- 1D Datastructure for storing Objects], Operations"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#### Creating Lists [] ####"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "#1a.\n",
- "list_num=[1,2,3]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3]\n"
- ]
- }
- ],
- "source": [
- "print (list_num)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "#1b.using range method\n",
- "list_range=list(range(0,5))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0, 1, 2, 3, 4]\n"
- ]
- }
- ],
- "source": [
- "print(list_range)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "#1c,using range method step argument\n",
- "list_range_step=list(range(0,5,2))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[0, 2, 4]"
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "print(list_range_step)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "[0, 1, 2, 3, 4]"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "#1d.List Comprehension, creating a list from another list by applying opertaions on elements of source List\n",
- "print(list_range) #src List"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0, 5, 10, 15, 20]\n"
- ]
- }
- ],
- "source": [
- "list_compre=[x*5 for x in list_range]\n",
- "print(list_compre)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3, 4, 5]\n"
- ]
- }
- ],
- "source": [
- "#1e.Using map function, maipulate each item of a source list using a defined function and create a new list\n",
- "def addOne(x):\n",
- " return x+1;\n",
- "\n",
- "range_list_plus_one=list(map(addOne,list_range))\n",
- "print(range_list_plus_one)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0, 2, 4]\n"
- ]
- }
- ],
- "source": [
- "#1f.using filter function,filter out elements and create a new list from source list\n",
- "def evenMembers(x):\n",
- " if x % 2==0:\n",
- " return True\n",
- " else:\n",
- " return False\n",
- " \n",
- "filtered_out_list=list(filter(evenMembers,list_range))\n",
- "print(filtered_out_list)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0, 2, 4]\n"
- ]
- }
- ],
- "source": [
- "#1g.Lambda expression , anonymous function , used for brevity , you can implement the above evenMembers function inline \n",
- "filtered_out_list_using_lambda=list(filter(lambda x:x%2==0,list_range))\n",
- "print(filtered_out_list_using_lambda)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#### Adding Element to List ####"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 32,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0, 2, 4, 9]\n"
- ]
- }
- ],
- "source": [
- "filtered_out_list_using_lambda.append(9)\n",
- "print(filtered_out_list_using_lambda)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#### Updating element of a list ####"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 33,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0, 2, 4, 90]\n"
- ]
- }
- ],
- "source": [
- "filtered_out_list_using_lambda[3]=90 #3 is the index not element value\n",
- "print(filtered_out_list_using_lambda)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#### Removing element from a List ####"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 34,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0, 2, 4]\n"
- ]
- }
- ],
- "source": [
- "filtered_out_list_using_lambda.remove(90) # removes the first instance of 90\n",
- "print(filtered_out_list_using_lambda)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#### Selecting a sublist : notation ####"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 35,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0, 5, 10, 15, 20]\n"
- ]
- }
- ],
- "source": [
- "print(list_compre)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 36,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[10, 15]\n"
- ]
- }
- ],
- "source": [
- "subList=list_compre[2:4] # start from 2nd index till 3rd\n",
- "print(subList)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#### Nested List ####"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 38,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3, 4, ['a', 'b', 'c', 'd']]\n"
- ]
- }
- ],
- "source": [
- "nested_alpha_numeric_list = [1,2,3,4,['a','b','c','d']]\n",
- "print(nested_alpha_numeric_list)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#### Accessing particular element in a nested List using [][] notations ####"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 39,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "b\n"
- ]
- }
- ],
- "source": [
- "print(nested_alpha_numeric_list[4][1]) ##accesses 'b' of nested list"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "#### Iterating a List item by item ####"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 41,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0\n",
- "5\n",
- "10\n",
- "15\n",
- "20\n"
- ]
- }
- ],
- "source": [
- "for item in list_compre:\n",
- " print(item)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment