|
383 | 383 | "cell_type": "markdown",
|
384 | 384 | "metadata": {},
|
385 | 385 | "source": [
|
386 |
| - "#### Overriding values\n", |
| 386 | + "#### Overriding list values\n", |
387 | 387 | "Alternatively, you can override elements by specifying an index like this:\n",
|
388 | 388 | "\n",
|
389 | 389 | "`list[index] = new value`"
|
|
693 | 693 | "\n",
|
694 | 694 | "print(sum(string_list)) # will return an error\n"
|
695 | 695 | ]
|
| 696 | + }, |
| 697 | + { |
| 698 | + "cell_type": "markdown", |
| 699 | + "metadata": {}, |
| 700 | + "source": [ |
| 701 | + "## Functions for Dictionaries\n", |
| 702 | + "These functions are specific for dictionaries." |
| 703 | + ] |
| 704 | + }, |
| 705 | + { |
| 706 | + "cell_type": "markdown", |
| 707 | + "metadata": {}, |
| 708 | + "source": [ |
| 709 | + "### keys and values\n", |
| 710 | + "Using the `keys()` and `values()` functions, we can get *only* the keys or the values from a given dictionary. This essentially returns a list of those values." |
| 711 | + ] |
| 712 | + }, |
| 713 | + { |
| 714 | + "cell_type": "code", |
| 715 | + "execution_count": 14, |
| 716 | + "metadata": {}, |
| 717 | + "outputs": [ |
| 718 | + { |
| 719 | + "name": "stdout", |
| 720 | + "output_type": "stream", |
| 721 | + "text": [ |
| 722 | + "dict_keys(['Werner', 'Niels', 'Enrico'])\n", |
| 723 | + "dict_values(['Heisenberg', 'Bohr', 'Fermi'])\n" |
| 724 | + ] |
| 725 | + } |
| 726 | + ], |
| 727 | + "source": [ |
| 728 | + "dict_test = {\n", |
| 729 | + " \"Werner\":\"Heisenberg\",\n", |
| 730 | + " \"Niels\":\"Bohr\",\n", |
| 731 | + " \"Enrico\":\"Fermi\"\n", |
| 732 | + "}\n", |
| 733 | + "\n", |
| 734 | + "print(dict_test.keys())\n", |
| 735 | + "print(dict_test.values())" |
| 736 | + ] |
| 737 | + }, |
| 738 | + { |
| 739 | + "cell_type": "markdown", |
| 740 | + "metadata": {}, |
| 741 | + "source": [ |
| 742 | + "### Overriding Dictionary Values\n", |
| 743 | + "Similar to [lists](#overriding-list-values), one can override a value of a given key in a dict.\n", |
| 744 | + "\n", |
| 745 | + "`dict[\"key\"] = \"new value\"`" |
| 746 | + ] |
| 747 | + }, |
| 748 | + { |
| 749 | + "cell_type": "code", |
| 750 | + "execution_count": 15, |
| 751 | + "metadata": {}, |
| 752 | + "outputs": [ |
| 753 | + { |
| 754 | + "name": "stdout", |
| 755 | + "output_type": "stream", |
| 756 | + "text": [ |
| 757 | + "{'Werner': 'Heisenberg', 'Niels': 'Not so Bohr', 'Enrico': 'Fermi'}\n" |
| 758 | + ] |
| 759 | + } |
| 760 | + ], |
| 761 | + "source": [ |
| 762 | + "dict_test[\"Niels\"] = \"Not so Bohr\"\n", |
| 763 | + "\n", |
| 764 | + "print(dict_test)" |
| 765 | + ] |
| 766 | + }, |
| 767 | + { |
| 768 | + "cell_type": "markdown", |
| 769 | + "metadata": {}, |
| 770 | + "source": [ |
| 771 | + "# Traversing a Data Structure\n", |
| 772 | + "Traversing means \"going through\". \n", |
| 773 | + "For data structures, we can traverse through the list and get all the values using **loops**." |
| 774 | + ] |
| 775 | + }, |
| 776 | + { |
| 777 | + "cell_type": "code", |
| 778 | + "execution_count": 4, |
| 779 | + "metadata": {}, |
| 780 | + "outputs": [ |
| 781 | + { |
| 782 | + "name": "stdout", |
| 783 | + "output_type": "stream", |
| 784 | + "text": [ |
| 785 | + "cab\n", |
| 786 | + "taxi\n", |
| 787 | + "hocus pocus\n" |
| 788 | + ] |
| 789 | + } |
| 790 | + ], |
| 791 | + "source": [ |
| 792 | + "list_trav = [\"cab\", \"taxi\", \"hocus pocus\"]\n", |
| 793 | + "\n", |
| 794 | + "for i in list_trav:\n", |
| 795 | + " print(i)" |
| 796 | + ] |
| 797 | + }, |
| 798 | + { |
| 799 | + "cell_type": "code", |
| 800 | + "execution_count": 5, |
| 801 | + "metadata": {}, |
| 802 | + "outputs": [ |
| 803 | + { |
| 804 | + "name": "stdout", |
| 805 | + "output_type": "stream", |
| 806 | + "text": [ |
| 807 | + "1\n", |
| 808 | + "3\n", |
| 809 | + "4\n" |
| 810 | + ] |
| 811 | + } |
| 812 | + ], |
| 813 | + "source": [ |
| 814 | + "tup_trav = (1, 3, 4)\n", |
| 815 | + "\n", |
| 816 | + "for i in tup_trav:\n", |
| 817 | + " print(i)" |
| 818 | + ] |
| 819 | + }, |
| 820 | + { |
| 821 | + "cell_type": "code", |
| 822 | + "execution_count": 12, |
| 823 | + "metadata": {}, |
| 824 | + "outputs": [ |
| 825 | + { |
| 826 | + "name": "stdout", |
| 827 | + "output_type": "stream", |
| 828 | + "text": [ |
| 829 | + "grave\n", |
| 830 | + "time\n", |
| 831 | + "lol\n", |
| 832 | + "yard\n", |
| 833 | + "stamp\n", |
| 834 | + "lmao\n" |
| 835 | + ] |
| 836 | + } |
| 837 | + ], |
| 838 | + "source": [ |
| 839 | + "dict_trav = {\n", |
| 840 | + " \"grave\":\"yard\",\n", |
| 841 | + " \"time\":\"stamp\",\n", |
| 842 | + " \"lol\":\"lmao\"\n", |
| 843 | + "}\n", |
| 844 | + "\n", |
| 845 | + "for i in dict_trav.keys(): # for only the keys, this is essentially a list\n", |
| 846 | + " print(i)\n", |
| 847 | + "\n", |
| 848 | + "for i in dict_trav.values(): # for only the values\n", |
| 849 | + " print(i)\n" |
| 850 | + ] |
696 | 851 | }
|
697 | 852 | ],
|
698 | 853 | "metadata": {
|
|
0 commit comments