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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:28:15+00:00 2026-06-13T21:28:15+00:00

Is there a way to efficiently concatenate str and list? inside = [] #a

  • 0

Is there a way to efficiently concatenate str and list?

inside = [] #a list of Items

class Backpack:
    def add(toadd):
        inside += toadd

print "Your backpack contains: " #now what do I do here?
  • 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-13T21:28:16+00:00Added an answer on June 13, 2026 at 9:28 pm

    It sounds like you’re just trying to add a string to a list of strings. That’s just append:

    >>> inside = ['thing', 'other thing']
    >>> inside.append('another thing')
    >>> inside
    ['thing', 'other thing', 'another thing']
    

    There’s nothing specific here to strings; the same thing works for a list of Item instances, or a list of lists of lists of strings, or a list of 37 different things of 37 different types.

    In general, append is the most efficient way to concatenate a single thing onto the end of a list. If you want to concatenate a bunch of things, and you already have them in a list (or iterator or other sequence), instead of doing them one at a time, use extend to do them all at once, or just += instead (which means the same thing as extend for lists):

    >>> inside = ['thing', 'other thing']
    >>> in_hand = ['sword', 'lamp']
    >>> inside += in_hand
    >>> inside
    ['thing', 'other thing', 'sword', 'lamp']
    

    If you want to later concatenate that list of strings into a single string, that’s the join method, as RocketDonkey explains:

    >>> ', '.join(inside)
    'thing, other thing, another thing'
    

    I’m guessing you want to get a little fancier and put an “and” between the last to things, skip the commas if there are fewer than three, etc. But if you know how to slice a list and how to use join, I think that can be left as an exercise for the reader.

    If you’re trying to go the other way around and concatenate a list to a string, you need to turn that list into a string in some way. You can just use str, but often that won’t give you what you want, and you’ll want something like the join example above.

    At any rate, once you have the string, you can just add it to the other string:

    >>> 'Inside = ' + str(inside)
    "Inside = ['thing', 'other thing', 'sword', 'lamp']"
    >>> 'Inside = ' + ', '.join(inside)
    'Inside = thing, other thing, another thing'
    

    If you have a list of things that aren’t strings and want to add them to the string, you have to decide on the appropriate string representation for those things (unless you’re happy with repr):

    >>> class Item(object):
    ...   def __init__(self, desc):
    ...     self.desc = desc
    ...   def __repr__(self):
    ...     return 'Item(' + repr(self.desc) + ')'
    ...   def __repr__(self):
    ...     return self.desc
    ...
    >>> inside = [Item('thing'), Item('other thing')]
    >>> 'Inside = ' + repr(inside)
    ... "Inside = [Item('thing'), Item('other thing')]"
    >>> 'Inside = ' + str(inside)
    ... "Inside = [Item('thing'), Item('other thing')]"
    >>> 'Inside = ' + ', '.join(str(i) for i in inside)
    ... 'Inside = thing, other thing'
    

    Notice that just calling str on a list of Items calls repr on the individual items; if you want to call str on them, you have to do it explicitly; that’s what the str(i) for i in inside part is for.

    Putting it all together:

    class Backpack:
        def __init__(self):
            self.inside = []
        def add(self, toadd):
            self.inside.append(toadd)
        def addmany(self, listtoadd):
            self.inside += listtoadd
        def __str__(self):
            return ', '.join(str(i) for i in self.inside)
    
    pack = Backpack()
    pack.add('thing')
    pack.add('other thing')
    pack.add('another thing')
    print 'Your backpack contains:', pack
    

    When you run this, it will print:

    Your backpack contains: thing, other thing, another thing
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to efficiently iterate over the values/items in a dictionary that
Is there a way to compute efficiently the Fourier transform of the max of
Is there a more efficient way to list files from a bucket in Amazon
Is there a way to efficiently change hue of a 2D OpenGL texture using
Is there a way to get efficiently the number of pages of a word
Is there a way to configure log4net to print logs both to console and
Is there a way to efficiently read previous versions of files in Git? (I'm
Is there a way to efficiently implement a rolling window for 1D arrays in
Is there a way to efficiently convert Word 2003 documents to Word 2007 without
Is there a way to efficiently share or move .NET objects across AppDomains? 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.