Delete list content in python

Source: Internet
Author: User
Here we use two examples to analyze how to use python to delete the list content. We also provide ideas to achieve this through pop and remove methods, respectively, for more information, see. A little busy today

a=['XXXX_game.sql', 'XXXX_game_sp.sql', 'XXXX_gamelog_sp.sql', 'XXXX_gamelog.sql']for i in a:  if 'gamelog' in i:    a.remove(i)print a['XXXX_game.sql', 'XXXX_game_sp.sql', 'XXXX_gamelog.sql']

The 'xxxx _ gamelog. SQL 'project was obviously missing during the review process. You can try it on your own. Why is it not deleted? Why?

We verify again

for i in a:  if 'gamelog' in i:    print i, XXXX_gamelog_sp.sql XXXX_gamelog.sql

If we don't perform the remove operation on the result, there will be no problem. It can be fully experienced.

In this way, we will probably know that it is impossible to use the calendar method to remove the list. So how to solve it?

A1 = a [:] # Here we mirror a list a1, but never use a1 = a. Why can we test a1 = a [:]? a1 = a True; a1 is a False; If a1 = a a1 = a True; a1 is a True, you can test it. This is a feature of the list. For I in a1: if 'gamelog 'in I: a. remove (I) print a ['xxxx _ game. SQL', 'xxxx _ game_sp. SQL ']

Another example

[{'Num': '001', 'name': 'zhang san', 'workingtime': 'monday', 'money': '123 '}
{'Num': '002 ', 'name': 'lily', 'workingtime': 'tuesday', 'money': '000000'}]
Because 'zhang san' exists, delete {'num': '001', 'name': 'zhang san', 'workingtime': 'monday', 'money ': '000000'} how to operate the entire row

The idea is to find the index of the element to be deleted in the list, and then call pop, the index as the parameter. Pop returns the deleted element. The rest of the queue is the rest after the index element is deleted.

Lname = [{'num': '001', 'name': 'zhang san', 'workingtime': 'monday', 'money ': '000000'} {'num': '002', 'name': 'lily', 'workingtime': 'tuesday', 'money ': '200'}] for x in range (len (lname): # list traversal if l [x] ['name'] = u 'zhangsan': lname. pop (x) # Use pop. Break # after the operation is completed, break goes out

Now, we will be here today.

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.