How to get dictionary type elements of Python 2.7

How to get Python 2.7 dictionary type elements

I don't know much about dictionaries, so it's a memo. 2.7 is because MotionBuilder is the main environment in which I use Python.

in key in dictionary

dictionary = {"key1":"value1","key2":"value2","key3":"value3"}
print("key1" in dictionary)

#True

Bool returns whether the item with the specified key exists. key in dictionary.values()

dictionary = {"key1":"value1","key2":"value2","key3":"value3"}
print ("key1" in dictionary.values())
print ("value1" in dictionary.values())

#False
#True

Here, bool returns whether the specified Value exists.

Get by slicing and get ()

dictionary[key]

dictionary = {"key1":"value1","key2":"value2","key3":"value3"}
print (dictionary["key1"])

#value1
```py
 Slice is the acquisition with []
dictionary.get(key)

dictionary = {"key1":"value1","key2":"value2","key3":"value3"} print (dictionary.get("key1"))

#value1

 There is no difference in the fact that the value can be obtained in both cases, but the behavior when there is no target is different.
 Error is returned for slices, None is returned for get ()

```py

print (dictionary.get("key4"))

#None

It is also possible to prepare the return value in advance when the specified key does not exist

print (dictionary.get("key4","no value"))

#no value

keys()

dictionary = {"key1":"value1","key2":"value2","key3":"value3"}
print(dictionary.keys())

#['key3', 'key2', 'key1']

The value is retrieved in a list. I did some research to find out why it was obtained from the end, but I don't know. If anyone knows, please let me know.

value()

dictionary = {"key1":"value1","key2":"value2","key3":"value3"}
print(dictionary.values())

#['value3', 'value2', 'value1']

The value is also obtained in the list

items()


dictionary = {"key1":"value1","key2":"value2","key3":"value3"}
print(dictionary.items())

[('key3', 'value3'), ('key2', 'value2'), ('key1', 'value1')]

You can get key and value at the same time. Each element in the list is a tuple.

iteritems()


dictionary = {"key1":"value1","key2":"value2","key3":"value3"}
for k, v in dictionary.iteritems():
    print k,v

#key3 value3
#key2 value2
#key1 value1

You can also get the key and value at the same time. It seems to use this when turning with a for statement. Items () of python3 series seems to return here by default.

Summary

I was not good at dictionary type, but I feel that I was able to organize it a little by myself by putting it together in this way. I want to check again the case where the value is obtained from the end.

Referenced page

https://qiita.com/jinshi/items/9f0f7ae716bce77a1b8a https://www.headboost.jp/python-dict-keys-tips/

Recommended Posts

How to get dictionary type elements of Python 2.7
How to write a list / dictionary type of Python3
[python] Summary of how to retrieve lists and dictionary elements
How to get the number of digits in Python
How to get the Python version
How to get started with Python
How to get a list of built-in exceptions in python
[Python] Summary of how to use pandas
How to get rid of long comprehensions
How to get a stacktrace in python
[Python2.7] Summary of how to use unittest
[Introduction to Udemy Python3 + Application] 24. Dictionary type
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
[Question] How to use plot_surface of python
[Python] How to get divisors of natural numbers at high speed
[Python] How to get the first and last days of the month
How to check the memory size of a dictionary in Python
[Introduction to Udemy Python 3 + Application] 26. Copy of dictionary
How to install Python
Summary of how to import files in Python 3
Get multiple maximum keys in Python dictionary type
Summary of how to use MNIST in Python
[Python] How to use list 2 Reference of list value, number of elements, maximum value, minimum value
How to get the files in the [Python] folder
How to use dictionary {}
How to handle datetime type in python sqlite3
How to remove duplicate elements in Python3 list
[Python] How to create a dictionary type list, add / change / delete elements, and extract with a for statement
Note: How to get the last day of the month with python (added the first day of the month)
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to get the variable name itself in python
How to convert Python # type for Python super beginners: str
I tried to summarize how to use matplotlib of python
Summary of Python sort (list, dictionary type, Series, DataFrame)
How to use Python Kivy ① ~ Basics of Kv Language ~
[Introduction to Udemy Python3 + Application] 53. Dictionary of keyword arguments
Get the size (number of elements) of UnionFind in Python
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
How to get mouse wheel verdict with Python curses
[Python] Summary of how to specify the color of the figure
Python # How to check type and type for super beginners
[Python] I tried to get Json of squid ring 2
How to install Python [Windows]
python3: How to use bottle (2)
How to update Python Tkinter to 8.6
Python --Check type of values
How to use Python argparse
[Python] How to use checkio
How to run Notepad ++ Python
How to change Python version
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
Python> dictionary> get ()> optional value
How to use Python bytes