Notes on Python and dictionary types

Note about python dictionary type

dic = {"Mandarin orange":2,"Apple":10,"Strawberry":2}

Take out the key

dic.keys()
dict_keys(['Mandarin orange', 'Apple', 'Strawberry', 'melon'])

Retrieve value

dic.values()
dict_values([2, 10, 2, 44])

Extract both key and value

dic.items()
dict_items([('Mandarin orange', 2), ('Apple', 10), ('Strawberry', 2), ('melon', 44)])

Extract both key and value with for statement

for i,k in dic.items():
    print(i,k)
Mandarin orange 2
Apple 10
Strawberry 2
Melon 44

Add new keys and values.

dic['Grape'] = 10
dic.items()
dict_items([('Mandarin orange', 2), ('Apple', 10), ('Strawberry', 2), ('melon', 44), ('Grape', 10)])

Recommended Posts

Notes on Python and dictionary types
Notes on building Python and pyenv on Mac
Notes on installing Python3 and using pip on Windows7
Notes on tf.function and Tracing
Python on Ruby and angry Ruby on Python
Notes on * args and ** kargs
Notes on pyenv and Atom
[Python] Notes on data analysis
Notes on installing Python on Mac
Notes on installing Python on CentOS
Notes on HDR and RAW image processing with Python
Python 3.6 on Windows ... and to Xamarin.
Notes on using MeCab from Python
Notes on using post-receive and post-merge
Integrate Modelica and Python on Windows
Notes on installing Python using PyEnv
Notes on using rstrip with python.
Notes on accessing dashDB from python
Python dictionary
Notes on deploying pyenv with Homebrew and managing Python versions
(Personal notes) Python metaclasses and metaprogramming
[Python] dictionary
[Python] Notes on while statements (writing style and infinite loop)
Python dictionary
Notes on reading and writing float32 TIFF images in python
Notes for using OpenCV on Windows10 Python 3.8.3.
Notes using cChardet and python3-chardet in Python 3.3.1.
Python and Bash on Cisco Catalyst IOS-XE
Notes on PyQ machine learning python grammar
Build Python3 and OpenCV environment on Ubuntu 18.04
Notes on nfc.ContactlessFrontend () for nfcpy in python
Python virtual environment and packages on Ubuntu
A memo with Python2.7 and Python3 on CentOS
Notes on doing Japanese OCR with Python
(Python) HTML reading and regular expression notes
See file and folder information on python
Understand python lists, dictionaries, and so on.
Install pyenv and Python 3.6.8 on Ubuntu 18.04 LTS
Notes on using code formatter in Python
[Python] Memo dictionary
Python scraping notes
Python study notes _000
Python learning notes
Python on Windows
twitter on python3
Python beginner notes
Python study notes_006
[Python] Dictionary (hash)
Notes on Flask
Python basics: dictionary
python C ++ notes
python on mac
Python study notes _005
Python grammar notes
Python Library notes
Notes on setting pyenv and python environment using Homebrew on Mac OS Marvericks
Read the file with python and delete the line breaks [Notes on reading the file]
Python on Windbg
python personal notes
python pandas notes
Python study notes_001