Python dictionary operations

Source: Internet
Author: User

Python dictionary operations

A dictionary in python is a ing type, which is implemented through a hash table, and a hash table is a data structure: It works as required. Each piece of data stored in a hash table is called a value. It is stored based on a data item associated with it that is called a key. Keys and values are collectively referred to as key-value pairs ). The hash table algorithm is used to obtain keys. The key performs an operation called a hash function. Based on the calculation result, select an address in the data structure to store your values. The address of any value storage depends on its key. Because of this randomness, the values in the hash table are unordered. You have an unordered dataset. Hash Tables have good performance, and it is quite fast to query values with keys.

Ii. operations related to the python dictionary:

1. Create a dictionary

# Method 1 dict1 = {'name': 'linda ', 'age': 13}
# Method 2 dict2 = dict (['name', 'linda '], ['age', 13])
# Method 3: Use the value of an Iterated object as each key of the dictionary and assign a value of 1dict3 = {}. fromkeys ('x', 'y', 'z'), 1)

2. Access the dictionary

# Return the list of keys dict1.keys () ['name', 'age']
# Return a list of values: dict1.values () ['linda ', 13]
# Return a list of (Key, value) tuples dict1.items () [('name', 'linda '), ('age': 13)]
# Value dict1 ['name'] 'linda 'corresponding to the Return key name'

# Obtain the key value. If the key does not exist, the default value dict1.get ('name', None) 'linda 'is returned'

# If no sex is in the dictionary, the default value of mandict1.get ('sex', 'man') 'man' dict is returned. iter () # Methods iteritems (), iterkeys (), itervalues () are the same as their non-iterative methods. The difference is that they return an iterator instead of a list.

3. Modify the dictionary

# Modify the value of the key name dict1 ['name'] = 'lihua' # If the key in the dictionary exists, delete and return the dict [key]. If the key does not exist, the default value is not provided, causing KeyError dict1.pop (name, 'unknown ') 'linda' # Delete the key name record dict1.remove ('name ') # If the dictionary does not contain a key, dict [key] = default assigns a value to it. If yes, the value dict1.setdefault ('name', default = 'unknown ') is not changed ') # Add the key-value pairs of the dictionary dict2. update (dict2) # delete all the elements in the dictionary. clear () # Delete the dictionary del dict1

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.