[python] Summary of how to retrieve lists and dictionary elements

Introduction

--A memorandum on how to retrieve the elements of the ``` list and dictionary --There are many other things such as adding and deleting elements, but here we will summarize only how to retrieve elements. ――What I found useful while using it is that you can combine `list` and `dictionary`. ――In particular, I personally use it most often because I can nest the `dictionary` inside the `dictionary`.

list

--A row of data of the same type --Each element is indexed in order from 0

Extract elements one by one by index

list.py


ls = ['Apple', 'Peaches', 'Cherry']
print(ls[0])
print(ls[1])
print(ls[2])

result


Apple
Peaches
Cherry

Extract elements one by one with the `` `for``` statement

list2.py


ls = ['Apple', 'Peaches', 'Cherry']
for a in ls:
    print(a)

result


Apple
Peaches
Cherry

dictionary

--A collection of data with keys and elements

Extract elements one by one by specifying a key

dic.py


dic = {'fruits':'Apple', 'Vegetables':'onion'}
print(dic['fruits'])
print(dic['Vegetables'])

result


Apple
onion

Extract the key with the `` `for``` statement

--You can get `key``` by using keys ``

dic2.py


dic = {'fruits':'Apple', 'Vegetables':'onion'}
for mykey in dic.keys():
    print(mykey)

result


fruits
Vegetables

Extract an element with a `` `for``` statement

--You can get `value``` by using values ``

dic2.py


dic = {'fruits':'Apple', 'Vegetables':'onion'}
for myval in dic.values():
    print(myval)

result


Apple
onion

Extract key and element at the same time with `` `for``` statement

--You can get both `key``` and `valueby usingitems```

dic3.py


dic = {'fruits':'Apple', 'Vegetables':'onion'}
for mykey, myval in dic.items():    
    print(mykey)
    print(myval)

result


fruits
Apple
Vegetables
onion

List in the dictionary

List value

--Elements can be retrieved with a combination of key and index

dic_list.py


dic = {'fruits':['Apple', 'Peaches'], 'Vegetables':['onion', 'carrot']}
#Extract a list of elements from a key
for mykey in dic.keys():
    for i in range(0,len(dic[mykey])):
        print(dic[mykey][i])

#Directly retrieve the list of elements
for myvalue in dic.values():
    for i in range(0,len(myvalue)):
        print(myvalue[i])

Both have the same result

result


fruits
Apple
Vegetables
onion

Dictionary in dictionary

Make `` `value``` a dictionary

--Elements can be retrieved with a combination of key and the nested dictionary `` `key```

dic_dic.py


dic = {'fruits': {'Like':'Apple', '大Like':'Peaches'}, 'Vegetables': {'Like':'Tomato', 'Hate':'green pepper'}}
print(dic['fruits']['Like'])
print(dic['Vegetables']['Hate'])

result


Apple
green pepper

Recommended Posts

[python] Summary of how to retrieve lists and dictionary elements
How to get dictionary type elements of Python 2.7
[Python] Summary of how to use split and join functions
[Python2.7] Summary of how to use unittest
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
How to compare lists and retrieve common elements in a list
Summary of how to import files in Python 3
Summary of how to use MNIST in Python
How to write a list / dictionary type of Python3
[Python] Summary of how to specify the color of the figure
How to swap elements in an array in Python, and how to reverse an array.
How to store Python function in Value of dictionary (dict) and call function according to Key
Summary of how to use pyenv-virtualenv
Summary of Hash (Dictionary) operation support for Ruby and Python
Comparison of how to use higher-order functions in Python 2 and 3
Summary of how to use csvkit
Summary of Python indexes and slices
Overview of Python virtual environment and how to create it
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
[Python] How to get the first and last days of the month
How to check the memory size of a dictionary in Python
How to sort 2D arrays, dictionaries and lists of proprietary classes
How to package and distribute Python scripts
How to install and use pandas_datareader [Python]
python: How to use locals () and globals ()
[Python] How to calculate MAE and RMSE
How to use Python zip and enumerate
How to use is and == in Python
Summary of how to write AWS Lambda
[Question] How to use plot_surface of python
[Python] How to specify the window display position and size of matplotlib
[Python] How to use list 2 Reference of list value, number of elements, maximum value, minimum value
Summary of how to set up major Python Lint (pep8, pylint, flake8)
[python] How to display list elements side by side
How to use lists, tuples, dictionaries, and sets
Correspondence summary of array operation of ruby and python
[Python] How to use two types of type ()
How to generate permutations in Python and C ++
Summary of the differences between PHP and Python
[Python] How to read data from CIFAR-10 and CIFAR-100
Rewriting elements in a loop of lists (Python)
Installation of Python3 and Flask [Environment construction summary]
How to convert SVG to PDF and PNG [Python]
How to specify attributes with Mock of python
[Python] How to use hash function and tuple.
[Python] How to create a dictionary type list, add / change / delete elements, and extract with a for statement
Summary of studying Python to use AWS Lambda
I / O related summary of python and fortran
List of Python code to move and remember
How to plot autocorrelation and partial autocorrelation in python
How to remove duplicate elements in Python3 list
Summary of how to read numerical data with python [CSV, NetCDF, Fortran binary]
How to count the number of elements in Django and output to a template
[Python Tips] How to retrieve multiple keys with the maximum value from the dictionary
How to install Python
How to install python
How to use dictionary {}
Summary of Python arguments
[Python] [Django] How to use ChoiceField and how to add options
Summary of tools needed to analyze data in Python