List 2
List 2
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "List2.ipynb",
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "-YyQsmNaWwdM"
},
"source": [
"**Change the second item**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "IvE8m-xZW3Tc"
},
"source": [
"thislist = [\"apple\", \"banana\", \"cherry\"]\n",
"thislist[1] = \"blackcurrant\"\n",
"print(thislist)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "yZvbwFKVW528"
},
"source": [
"**Change the values \"banana\" and \"cherry\" with the
values \"blackcurrant\" and \"watermelon\"**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "C3UB9JGXW9hd"
},
"source": [
"thislist =
[\"apple\", \"banana\", \"cherry\", \"orange\", \"kiwi\", \"mango\"]\n",
"thislist[1:3] = [\"blackcurrant\", \"watermelon\"]\n",
"print(thislist)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "QV99JD6UXA7O"
},
"source": [
"**Change the second value by replacing it with two new values**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "OqG9T2-AXFqW"
},
"source": [
"thislist = [\"apple\", \"banana\", \"cherry\"]\n",
"thislist[1:2] = [\"blackcurrant\", \"watermelon\"]\n",
"print(thislist)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "RClXUItjXIfW"
},
"source": [
"**Change the second and third value by replacing it with one value**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "CCZ_FU47XL_g"
},
"source": [
"thislist = [\"apple\", \"banana\", \"cherry\"]\n",
"thislist[1:3] = [\"watermelon\"]\n",
"print(thislist)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "wGZg4KukXS_Q"
},
"source": [
"**Insert \"watermelon\" as the third item**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "ykOgAW01XV_6"
},
"source": [
"thislist = [\"apple\", \"banana\", \"cherry\"]\n",
"thislist.insert(2, \"watermelon\")\n",
"print(thislist)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "8Cf94GsmXd9w"
},
"source": [
"**Using the append() method to append an item**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "FfTNxuaSXe9T"
},
"source": [
"thislist = [\"apple\", \"banana\", \"cherry\"]\n",
"thislist.append(\"orange\")\n",
"print(thislist)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "0uPZneHNXieS"
},
"source": [
"**Insert an item as the second position**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "P2DhwDX_Xnja"
},
"source": [
"thislist = [\"apple\", \"banana\", \"cherry\"]\n",
"thislist.insert(1, \"orange\")\n",
"print(thislist)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "Lkvo8653XwPr"
},
"source": [
"**Add the elements of tropical to thislist**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "z1cHg_FZXxD8",
"outputId": "8707fee9-2c7a-461a-e4f5-86597d373e6e"
},
"source": [
"thislist = [\"apple\", \"banana\", \"cherry\"]\n",
"tropical = [\"mango\", \"pineapple\", \"papaya\"]\n",
"thislist.extend(tropical)\n",
"print(thislist)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "JcRhSjrkX0Tk"
},
"source": [
"**Add elements of a tuple to a list**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "cL3YRLtbX721"
},
"source": [
"thislist = [\"apple\", \"banana\", \"cherry\"]\n",
"thistuple = (\"kiwi\", \"orange\")\n",
"thislist.extend(thistuple)\n",
"print(thislist)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "_YdmMAHHY0MW"
},
"source": [
"**Remove \"banana\"**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "IMRo3OO8Y0_Y"
},
"source": [
"thislist = [\"apple\", \"banana\", \"cherry\"]\n",
"thislist.remove(\"banana\")\n",
"print(thislist)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "RL1yqvcyY7pJ"
},
"source": [
"**Remove the second item**"
]
},
{
"cell_type": "code",
"source": [
"thislist = [\"apple\", \"banana\", \"cherry\"]\n",
"thislist.pop(1)\n",
"print(thislist)"
],
"metadata": {
"id": "hlEaG6h73TZu"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "F-vXbvANZAgv"
},
"source": [
"**Remove the last item**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "pAU4yrjrZFcR"
},
"source": [
"thislist = [\"apple\", \"banana\", \"cherry\"]\n",
"thislist.pop()\n",
"print(thislist)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "ttZEAj2zZLTS"
},
"source": [
"**Remove the first item**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "u06Q5aqIZNHw"
},
"source": [
"thislist = [\"apple\", \"banana\", \"cherry\"]\n",
"del thislist[0]\n",
"print(thislist)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "OePXvbL-ZQlA"
},
"source": [
"**Delete the entire list**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "bpkJARhbZT9p"
},
"source": [
"thislist = [\"apple\", \"banana\", \"cherry\"]\n",
"del thislist"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "yU7tde6lZWn5"
},
"source": [
"**Clear the list content**"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "e3SrhCgFZbN7",
"outputId": "726a3717-f5ae-418d-87c0-010095c6c970"
},
"source": [
"thislist = [\"apple\", \"banana\", \"cherry\"]\n",
"thislist.clear()\n",
"print(thislist)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[]\n"
]
}
]
}
]
}