Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7059079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:11:21+00:00 2026-05-28T04:11:21+00:00

So say i had some text like this: line num 1 line num 2

  • 0

So say i had some text like this:

line num 1
line num 2

line num 3



line num 4

I am trying to get rid of all the new lines in between line 2 and 3 and line 3 and 4 while having all of the line num on separate new lines. How would i accomplish this? I have already tried puth=ing them into a list then looping throught them and taking out all of the lone ‘\n’

ex:

obj=['line num 1','line num 2','\n','line num 3','\n','\n','line num4']
a=-1
for i in obj:
    a+=1
    if i=='\n':
        print 'yes'
        del obj[a]


print obj

output:

['line num 1', 'line num 2', 'line num 3', '\n', 'line num4']

It catches some but not all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:11:22+00:00Added an answer on May 28, 2026 at 4:11 am

    In short: don’t erase elements while iterating over a list.

    Here you will find lot of ways to do this: Remove items from a list while iterating

    Note: this is probably the shortest and most pythonic:

    filter(lambda x: x!='\n', obj)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say you had some resource clean up like: This is C#. try{/*stuff*/} catch(Exception
I had this idea... let's say I have a DIV that contains some text.
I had this doubt for sometime now, some people say that there's no such
Say I had a function such as this one procedure TMyObject.DoSomething(text: string); begin //
Say I have this <div id=x data-id='1' data-prop1='peace' data-prop2='out'>Text</div> in jQuery i can get
Let's say the original text is something like this: 12345 {unit} Then what's the
Let's say I had a program in C# that did something computationally expensive, like
I am developing a Facebook App in which I get some text from the
Can anyone suggest how I would do this: Say if I had the string
I would like to get some opinions on when/if it is ok to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.