Python Forum
how to remove none from the for loop
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to remove none from the for loop
#1
Dear all,
c= " domain ontologies"

mystr = c.split(' ')   
   
  c= [i for i in mystr[:1]]
        
 print(c)
result 
Output:
['domain', 'ontologies'] ['domain'] None
Reply
#2
[i for i in mystr[:1] if i]
You have an interval in the begening in the string " domain ontologies". So when you split it, it returns an empty string as the first index. Which is None in Python. Use strip().

mystr = c.strip(' ').split()
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
As pointed out by Ofnuts in the other thread split() without argument will take care.

>>> " domain ontologies".split()
['domain', 'ontologies']
Reply
#4
Use Code tag.
Code you have will not output what you have posted.
Reply
#5
(Feb-27-2017, 01:30 PM)desul Wrote: Dear all,

actually,  i want only first word of the term for that i am using below code but i am getting word with "none" , how can remove it or any other technique ....

  
c= "domain ontologies"

mystr = c.split(' ')   
   
  c= [i for i in mystr[:1]]
        
 print(c)
result 
Output:
['domain', 'ontologies'] ['domain'] None
thanking u .......
Reply
#6
Just repeating what others have said: 

c = " domain ontologies"

mystr = c.split()
print(mystr[0])
print(mystr[1])
Output:
domain ontologies
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyKML] Loop through all Placemarks; Remove namespace Winfried 2 3,455 Aug-28-2020, 09:24 AM
Last Post: Winfried
  python multiprocessing import Pool, cpu_count: causes forever loop | help to remove Hassibayub 0 1,879 Jun-18-2020, 05:27 PM
Last Post: Hassibayub

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020