Taboo for The For loop

Source: Internet
Author: User
Tags for in range

1. Error item

1 s=[11,22,33,44,55]2 for in range (Len (s)):  # will only happen once, Directly stored in memory, directly called.  3         print(i)        #  i=0                    i=1               i=2           i=34         del  s[i]       5         print(s)       #[ 22,33,44,55]    [22,44,55]      [22,44]     This time will be an error! 

2. The first method of solution

1s={"K1":"v1","K2":"v2","A3":"v3"}#This topic requires the deletion of the key value with K. 2s1={}#as excessive. 3  forIinchS:4     if "k"  not inchI#if there is no k in the key value:5S.setdefault (I,s[i])#Creates a new set of eligible key values into the S dictionary. At this point I is a variable, so you do not need to use quotation marks like new key-value pairs. 6S=s1#re-assigns the eligible key-value pairs to the original dictionary. This operation will completely overwrite the original dictionary without leaving any traces. 7 Print(s)

Taboo for The For loop

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.