{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6.2.4 Dictionary Methods `keys()` and `value()` " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "months = {'January': 1, 'February': 2, 'March': 3}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Dictionary method `keys()` returns the keys of a dictionary." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for month_name in months.keys():\n", " print(month_name, end=' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Dictionary method `values()` returns the values of a dictionary." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for month_number in months.values():\n", " print(month_number, end=' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionary Views" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Methods `key()` and `values()` returns a **view** of a dictionary's data that we can iterate over. A view is **not** a copy of the data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "months_view = months.keys()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for key in months_view:\n", " print(key, end=' ')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "months['December'] = 12" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We've added a new key-value pair." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "months" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We didn't change the view `months_view` but it can see the new pair." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for key in months_view:\n", " print(key, end=' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Converting Dictionary Keys, Values and Key–Value Pairs to Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We can create lists of a dictionary's data. The lists contains copies of the data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list(months.keys())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list(months.values())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list(months.items())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Processing Keys in Sorted Order " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use the built-in `sorted()` function to process keys in sorted order." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for month_name in sorted(months.keys()):\n", " print(month_name, end=' ')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##########################################################################\n", "# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n", "# Pearson Education, Inc. All Rights Reserved. #\n", "# #\n", "# DISCLAIMER: The authors and publisher of this book have used their #\n", "# best efforts in preparing the book. These efforts include the #\n", "# development, research, and testing of the theories and programs #\n", "# to determine their effectiveness. The authors and publisher make #\n", "# no warranty of any kind, expressed or implied, with regard to these #\n", "# programs or to the documentation contained in these books. The authors #\n", "# and publisher shall not be liable in any event for incidental or #\n", "# consequential damages in connection with, or arising out of, the #\n", "# furnishing, performance, or use of these programs. #\n", "##########################################################################\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Additional material (C) Copyright 2023 by Ronald Mak" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }