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

  • Home
  • SEARCH
  • 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 6375067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:34:00+00:00 2026-05-25T01:34:00+00:00

I have strings stored in various objects, for example a, b, c[2:7] (ie 5

  • 0

I have strings stored in various objects, for example a, b, c[2:7] (ie 5 different strings c[2],…c[6]), d.s, e.s[:] (ie all strings stored in the e.s list). I need to loop over all these strings on various places in the code and modify these variables in the loops. (Assigning the modified strings to new variables would not work as there are other functions expecting the modified strings in the original variables.) How can I do that efficiently in Python?

If I could create a list of references to the strings then I could just always iterate over it:

str_ref_lst=[reference(a), reference(b), reference(d.s)]
str_ref_lst.extend[reference(c[2:7])]
str_ref_lst.extend[reference(e.s[:])]
...
for ref_strng in str_ref_lst:
  do_string_modifications1(dereference(ref_strng))
...
function_using_the_modified_variables1()
...
for ref_strng in str_ref_lst:
  do_string_modifications2(dereference(ref_strng))
...
function_using_the_modified_variables2()
etc

However, I am not aware of reference()/dereference() analogues in Python (and I guess it would not be the right way in Python anyway)

  • 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-25T01:34:00+00:00Added an answer on May 25, 2026 at 1:34 am

    The recommended answer should be “use a dictionary”, but if you have to do it that way:

    class cls(object):
        pass
    
    a = "<a>"
    b = "<b>"
    c = ["", "", "<c>"]
    d = cls()
    d.s = "<d.s>"
    e = cls()
    e.s = "<e.s>"
    
    var_list = "a b c[2:7] d.s e.s".split()
    
    for var in var_list:
        exec "%s+='Mod'" % var             # Modify the variables
    
    print a,b,c,d.s,e.s
    # >>> <a>Mod <b>Mod ['', '', '<c>', 'M', 'o', 'd'] <d.s>Mod <e.s>Mod
    

    IIRC this way doesn’t work in Python 3.0 because of internal optimizations

    With slices, it should be somewhat like

    import re
    
    class cls(object):
        pass
    
    def fn(var):      # function to apply to the strings
        return "[%s mod]" % var
    
    a = "<a>"
    b = "<b>"
    c = "01234567890"
    d = cls()
    d.s = "<d.s>"
    e = cls()
    e.s = "<e.s>"
    
    var_list = "a b c[2:7] d.s e.s".split()
    
    for var in var_list:
        # Modify the variables
        try:
            # exec "%s='Hi there'" % var             # Modify the variables
            exec "%(var)s=fn(%(var)s)" % { "var": var }
        except TypeError:
            # Assume Slice...
            slice_ = re.findall("\[(\d+)\:(\d+)\]", var)[0]   # Get slice
            var_with_slice = var
            var = var.split("[")[0]                 # Remove slice
            # Look ma, I can code in Perl too! :)
            line = \
                 "%(var)s=%(var)s[:%(slice1)s]+fn(%(var_with_slice)s)+%(var)s[%(slice2)s+1:]" % \
                 { "var": var, "var_with_slice": var_with_slice, "slice1": slice_[0], "slice2": slice_[1] }
            # somewhat like c = c[:2] + fn(c[2:7]) + c[7+1:]
            exec line
    
    print a,b,c,d.s,e.s
    # >>> [<a> mod] [<b> mod] 01[23456 mod]890 [<d.s> mod] [<e.s> mod]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have values stored as strings in a DataTable where each value could really
If I have function names stored as strings in a Hashtable. Is there a
Overview: I have an array of 20 byte strings that needs to be stored
I have some C code that stores ASCII strings in memory as a four
Lets say you have various objects of arbitrary type that you would like to
I have various length strings which are full of Base64 chars. Actualy they are
I have an array of objects which contains various kinds of data in it,
I want to delete the various information i have stored as shared preferences. I
We have XML code stored in a single relational database field to get around
I have an Oracle XMLType column that stores the various language specific strings. I

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.