{ "cells": [ { "cell_type": "markdown", "id": "5b1d21eb", "metadata": {}, "source": [ "# Lisi Operations" ] }, { "cell_type": "code", "execution_count": 139, "id": "b8a7504b", "metadata": {}, "outputs": [], "source": [ "# List\n", "mylist = [\"apple\", \"banana\", \"cherry\"]" ] }, { "cell_type": "code", "execution_count": 140, "id": "22045eee", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'banana', 'cherry', 'apple', 'cherry']\n" ] } ], "source": [ "# print the list\n", "thislist = [\"apple\", \"banana\", \"cherry\", \"apple\", \"cherry\"]\n", "print(thislist)" ] }, { "cell_type": "code", "execution_count": 141, "id": "3de98cfc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "# Length of list\n", "thislist = [\"apple\", \"banana\", \"cherry\"]\n", "print(len(thislist))" ] }, { "cell_type": "code", "execution_count": 142, "id": "1644c599", "metadata": {}, "outputs": [], "source": [ "# different data types in list\n", "list1 = [\"apple\", \"banana\", \"cherry\"]\n", "list2 = [1, 5, 7, 9, 3]\n", "list3 = [True, False, False]" ] }, { "cell_type": "code", "execution_count": 143, "id": "ad86e453", "metadata": {}, "outputs": [], "source": [ "# different data types in a single list\n", "list1 = [\"abc\", 34, True, 40, \"male\"]" ] }, { "cell_type": "code", "execution_count": 144, "id": "0ef37cc4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "mylist = [\"apple\", \"banana\", \"cherry\"]\n", "print(type(mylist))" ] }, { "cell_type": "code", "execution_count": 145, "id": "0bd3956a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'banana', 'cherry']\n" ] } ], "source": [ "# Another way to create a list\n", "thislist = list((\"apple\", \"banana\", \"cherry\"))\n", "print(thislist)" ] }, { "cell_type": "code", "execution_count": 146, "id": "f51e0509", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'banana', 'orange']\n" ] } ], "source": [ "# Add Items to a List\n", "fruits = ['apple', 'banana']\n", "fruits.append('orange')\n", "print(fruits)" ] }, { "cell_type": "code", "execution_count": 147, "id": "d62d99d7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'mango', 'banana', 'orange']\n" ] } ], "source": [ "# insert(index, item) – Insert item at a specific index\n", "fruits.insert(1, 'mango')\n", "print(fruits)" ] }, { "cell_type": "code", "execution_count": 148, "id": "f947d134", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'mango', 'banana', 'orange', 'grape', 'pineapple']\n" ] } ], "source": [ "# extend(list) – Add multiple items from another list\n", "fruits.extend(['grape', 'pineapple'])\n", "print(fruits)" ] }, { "cell_type": "code", "execution_count": 149, "id": "24d3ecad", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['strawberry', 'mango', 'banana', 'orange', 'grape', 'pineapple']\n" ] } ], "source": [ "# Update by index\n", "fruits[0] = 'strawberry'\n", "print(fruits)" ] }, { "cell_type": "code", "execution_count": 150, "id": "b820261e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['strawberry', 'mango', 'banana', 'grape', 'pineapple']\n" ] } ], "source": [ "# remove(item) – Remove by value\n", "fruits.remove('orange')\n", "print(fruits)" ] }, { "cell_type": "code", "execution_count": 151, "id": "e8bde013", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pineapple\n", "['strawberry', 'mango', 'banana', 'grape']\n" ] } ], "source": [ "# pop(index) – Remove and return item by index (default is last)\n", "last_item = fruits.pop()\n", "print(last_item)\n", "print(fruits)" ] }, { "cell_type": "code", "execution_count": 152, "id": "1bd10b49", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['strawberry', 'banana', 'grape']\n" ] } ], "source": [ "# del – Delete by index or slice\n", "del fruits[1]\n", "print(fruits) " ] }, { "cell_type": "code", "execution_count": 153, "id": "368b7abf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "# clear() – Remove all items\n", "fruits.clear()\n", "print(fruits)" ] }, { "cell_type": "markdown", "id": "21857eeb", "metadata": {}, "source": [ "# Basic Practice problems" ] }, { "cell_type": "code", "execution_count": 154, "id": "8f7069a3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'banana', 'orange']\n" ] } ], "source": [ "# Given the list fruits = ['apple', 'banana'], add 'orange' at the end.\n", "fruits = ['apple', 'banana']\n", "fruits.append('orange')\n", "print(fruits)" ] }, { "cell_type": "code", "execution_count": 155, "id": "97680bab", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'mango', 'banana']\n" ] } ], "source": [ "# Insert 'mango' between 'apple' and 'banana'.\n", "fruits = ['apple', 'banana']\n", "fruits.insert(1, 'mango')\n", "print(fruits)" ] }, { "cell_type": "code", "execution_count": 156, "id": "21d5422f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'banana', 'grape', 'pineapple']\n" ] } ], "source": [ "# Add ['grape', 'pineapple'] to the list fruits.\n", "fruits = ['apple', 'banana']\n", "fruits.extend(['grape', 'pineapple'])\n", "print(fruits)" ] }, { "cell_type": "code", "execution_count": 163, "id": "6470364e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['strawberry', 'banana']\n" ] } ], "source": [ "# ange 'apple' to 'strawberry'.\n", "fruits = ['apple', 'banana']\n", "fruits[0] = 'strawberry'\n", "print(fruits)" ] }, { "cell_type": "code", "execution_count": 169, "id": "54de17a8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'orange']\n" ] } ], "source": [ "# Remove 'banana' from ['apple', 'banana', 'orange'].\n", "fruits = ['apple', 'banana', 'orange']\n", "fruits.remove('banana')\n", "print(fruits)" ] }, { "cell_type": "code", "execution_count": 170, "id": "6d81fe0f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'orange']\n" ] } ], "source": [ "# Delete the second item in the list.\n", "fruits = ['apple', 'banana', 'orange']\n", "del fruits[1]\n", "print(fruits)" ] }, { "cell_type": "code", "execution_count": 171, "id": "872276ff", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "orange\n", "['apple', 'banana']\n" ] } ], "source": [ "# Remove the last fruit from the list and store it in a variable.\n", "fruits = ['apple', 'banana', 'orange']\n", "last_fruit = fruits.pop()\n", "print(last_fruit)\n", "print(fruits)" ] }, { "cell_type": "code", "execution_count": 172, "id": "5d30b05f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "# Remove all items from ['a', 'b', 'c'].\n", "letters = ['a', 'b', 'c']\n", "letters.clear()\n", "print(letters)" ] } ], "metadata": { "kernelspec": { "display_name": "all", "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.12.9" } }, "nbformat": 4, "nbformat_minor": 5 }