1. The clear() method is used to clear all data in the dictionary. Because it is an in-situ operation, it returns None (which can also be understood as no return value)
>>> x['name'] = 'lili' >>> x['age'] = 20 >>> x {'age': 20, 'name': 'lili'} >>> returned_value = x.clear() >>> x { } >>> print returned_value None
What are the characteristics of the dictionary's clear() method:
>>> f = {'key':'value'} >>> a = f >>> a {'key': 'value'} >>> f.clear() >>> f {} >>> a {}
When the original dictionary is referenced, if you want to clear the elements in the original dictionary, use the clear() method, and the elements in the dictionary will also be cleared at the same time. .
The Python dictionary pop() method deletes the given key key and the corresponding value in the dictionary, and the return value is the deleted value. The key value must be given. Otherwise, return the default value.
2. Pop() method syntax:
pop(key[,default])
Parameters:
key: the key value to be deleted default: If there is no key, return the default value
Return value: Return the deleted value.
Example:
>>> x = {'a':1,'b':2} >>> x.pop('a') 1 >>> x {'b': 2}
3. The function of the dictionary popitem() method is to randomly return and delete a pair of keys and values (items) in the dictionary.
>>> x {'url': 'www.iplaypy.com', 'title': 'python web site'} >>> x.popitem() ('url', 'www.iplaypy.com') >>> x {'title': 'python web site'}
4. The del method can delete a single element and clear the dictionary. Clearing only requires one operation.
#!/usr/bin/python # -*- coding: UTF-8 -*- dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} del dict['Name'] # 删除键是'Name'的条目 del dict # 删除字典
For more Python-related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to delete elements in python dictionary. For more information, please follow other related articles on the PHP Chinese website!

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment