PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How can I get rid of elements containing exact symbols in the list? (python)

 May 12, 2022     append, list, python     No comments   

Issue

I have a list called "list", which consists of about 20K strings, and I need to remove from it elements which have "text": "" in it.

I'm creating a new clean list like this

clean_list = []

for i in list:
  if '"text": ""' in i == False:
    clean_list.append(i)
  print(i)

But elements don't append and clean_list is empty. What can be the problem? Smth is wrong with the cycle.

How else can I get rid of some elements in the list?


Solution

First, you should not name variables with protected keywords like list.

For your use case you could use list comprehension:

clean_list = [string for string in list if '"text": ""' not in string]


Answered By - nb123
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing