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 8603375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:18:30+00:00 2026-06-12T02:18:30+00:00

okay here is the example: data = [‘This’, ‘is’, ‘a’, ‘test’, ‘of’, ‘the’, ‘list’]

  • 0

okay here is the example:

data = ['This', 'is', 'a', 'test', 'of', 'the', 'list']
replaceText = 'test'
replaceData =['new', 'test']

i did data.replace(replaceText, replaceData) but it doesn’t work. How to replace a string in a list of string with a list of strings? Any help will be appreciated.

Edit:
The exact condition is to replace or split the words that contain “s” so I put a loop in it. So the end result will print
data = [‘Thi’, ‘i’, ‘a’, ‘te’,’t’, ‘of’, ‘the’, ‘li’,’t’]

  • 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-06-12T02:18:31+00:00Added an answer on June 12, 2026 at 2:18 am

    In a list, find the position of text with .index(), then replace by using slice assignment:

    pos = data.index(replaceText)
    data[pos:pos+1] = replaceData
    

    This will replace only one occurrence of replaceText at a time. Demo:

    >>> data = ['This', 'is', 'a', 'test', 'of', 'the', 'list']
    >>> replaceText = 'test'
    >>> replaceData =['new', 'test']
    >>> pos = data.index(replaceText)
    >>> data[pos:pos+1] = replaceData
    

    To replace all occurences, use pos plus the length of replaceData to skip searching past the previous match:

    pos = 0
    while True:
        try:
            pos = data.index(replaceText, pos)
        except ValueError:
            break
        data[pos:pos+1] = replaceData
        pos += len(replaceData)
    

    If you need to loop over data while modifying it, use a copy instead:

    for n in data[:]:
        # maniplate data
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay this is definitely a n00b question but here goes. The way I understand
Okay, I'm horrendously new to MySQL and PHP, but could use some help here.
Okay basically here's where I'm at. I have a list of PropertyDescriptor objects. These
Okay, Now I have an unordered list here: <ul id="mycustomid"> <li><a href="url of Item
This seems silly, but I don't understand how Django Templates access nested data in
I'm using QueryPath and PHP. This finds the .eventdate okay, but doesn't return anything
Haskell here we go! Okay so here is an example of the code currently
Hello kind computing people, Okay this is super broad, but I thought I'd give
Okay the title may not justify what I'm trying to do here but dont
Okay, it appeared that I had an answer for this question, but I was

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.