How to use popitem method in autotest

Best Python code snippet using autotest_python

12.字典的使用.py

Source:12.字典的使用.py Github

copy

Full Screen

...48# print(d)49# 删除,可以使用 del 来删除字典中的 key-value50del d['a']51del d['b']52# popitem()53# 随机删除字典中的一个键值对,一般都会删除最后一个键值对54# 删除之后,它会将删除的key-value作为返回值返回55# 返回的是一个元组,元组中有两个元素,第一个元素是删除的key,第二个是删除的value56# 当使用popitem()删除一个空字典时,会抛出异常 KeyError: 'popitem(): dictionary is empty'57# d.popitem()58# result = d.popitem()59# pop(key[, default])60# 根据key删除字典中的key-value61# 会将被删除的value返回!62# 如果删除不存在的key,会抛出异常63# 如果指定了默认值,再删除不存在的key时,不会报错,而是直接返回默认值64result = d.pop('d')65result = d.pop('z','这是默认值')66# del d['z'] z不存在,报错67# result = d.popitem()68# result = d.popitem()69# result = d.popitem()70# result = d.popitem()71# clear()用来清空字典72d.clear()73# print('result =',result)74# print(d)75# copy()76# 该方法用于对字典进行浅复制77# 复制以后的对象,和原对象是独立,修改一个不会影响另一个78# 注意,浅复制会简单复制对象内部的值,如果值也是一个可变对象,这个可变对象不会被复制79d = {'a':1,'b':2,'c':3}80d2 = d.copy()81# d['a'] = 10082d = {'a':{'name':'孙悟空','age':18},'b':2,'c':3}83d2 = d.copy()84d2['a']['name'] = '猪八戒'...

Full Screen

Full Screen

popitem()_method.py

Source:popitem()_method.py Github

copy

Full Screen

1"""2-->The popitem() method removes and returns the last element(key, value) pair inserted into the dictionary.3syntax : dict.popitem()4The popitem() method removes and returns the (key, value) pair from the dictionary in the Last In, First Out (LIFO) order. Returns the latest inserted element (key,value) pair from the dictionary.5Removes the returned element pair from the dictionary.6Note: Before Python 3.7, the popitem() method returned and removed an arbitrary element (key, value) pair from the dictionary.7example:8---------9dict1 = {'eid' : 123, 'ename' : 'Guido', 'salary' : 10000, 'Nationality' : 'USA'}10print('removing last key value : ', dict1.popitem())11print('new dictionary : ', dict1)12output:13-------14removing last key value : ('Nationality', 'USA')15new dictionary : {'eid': 123, 'ename': 'Guido', 'salary': 10000}16example:17--------18dict1 = {'eid' : 123, 'ename' : 'Guido', 'salary' : 10000, 'Nationality' : 'USA'}19dict1.update({'a' : 20})20dict1.update({'b' : 30})21print('removing last key value : ', dict1.popitem())22print('new dictionary : ', dict1)23output:24-------25removing last key value : ('b', 30)26new dictionary : {'eid': 123, 'ename': 'Guido', 'salary': 10000, 'Nationality': 'USA', 'a': 20}27example: CAN'T PERFORM THE POPITEM() ON THE EMPTY DICTIONARY.28--------29dict1 = {}30print('removing last key value : ', dict1.popitem())31print('new dictionary : ', dict1)32output:33-------34Traceback (most recent call last):35 File "C:\Users\this\Desktop\sample1.py", line 2, in <module>36 print('removing last key value : ', dict1.popitem())37KeyError: 'popitem(): dictionary is empty'38example:39--------40print('removing last key value : ', dict().popitem())41output:42-------43Traceback (most recent call last):44 File "C:\Users\this\Desktop\sample1.py", line 2, in <module>45 print('removing last key value : ', dict1.popitem())46KeyError: 'popitem(): dictionary is empty'...

Full Screen

Full Screen

_8_popitem.py

Source:_8_popitem.py Github

copy

Full Screen

1# popitem()------ method removes and returns the last element (key, value) pair inserted into the dictionary2print("*****popitem*****")3print("popitem()--->Removes the last inserted key-value pair")4# The popitem() method removes and returns the (key, value) pair from the5# dictionary in the Last In, First Out (LIFO) order.6marks = {'Physics': 67, 'Maths': 87, 'Hindi': 70, 'French': 45}7a = marks.popitem()8print("The popped item is:", a)9print("Updated entries:", marks)10# The popitem() method raises a KeyError error if the dictionary is empty11Employee = {"Name": "John", "Age": 29, "salary": 25000, "Company": "GOOGLE", 'Address': 'HP'}12print(type(Employee))13print("printing Employee data .... ")14print(Employee)15print('popitem is :', Employee.popitem())16print('Dictionary after popitem :', Employee)17print('popitem is :', Employee.popitem())18print('Dictionary after popitem :', Employee)19print('popitem is :', Employee.popitem())20print('Dictionary after popitem :', Employee)21d = {}22print('Dictionary :', d)23print('type :', type(d))24d[0] = 'zero'25d[1] = 'one'26d[2] = 'two'27d[3] = 'three'28d[4] = 'four'29print('Dictionary :', d)30print('popitem is :', d.popitem())31print('Dictionary after popitem :', d)32print('popitem is :', d.popitem())33print('Dictionary after popitem :', d)34print('popitem is :', d.popitem())...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful