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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:53:13+00:00 2026-05-27T08:53:13+00:00

I came accross this snippet: li = [‘a’, ‘b’, ‘c’] print \n.join(li) The author

  • 0

I came accross this snippet:

li = ['a', 'b', 'c']
print "\n".join(li)

The author says:

This is also a useful debugging trick when you’re working with lists.

What is the trick here?

source

  • 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-27T08:53:14+00:00Added an answer on May 27, 2026 at 8:53 am

    Printing a list like that is useless for debugging:

    (1) If any of the list items are not strings, you will get an exception.

    (2) Your stdout may not be able to display the strings, resulting in an exception or just gibberish.

    (3) You won’t see the difference between (for example) tabs (\t) and multiple spaces.

    Much better:

    Python 2.x : print repr(li)

    Python 3.x : print(ascii(li))

    Update Here’s what can happen with print(li') on Python 3.x (it’s problem 2 above):

    >>> li = ['\u0404']
    >>> print(li)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\python32\lib\encodings\cp850.py", line 19, in encode
        return codecs.charmap_encode(input,self.errors,encoding_map)[0]
    UnicodeEncodeError: 'charmap' codec can't encode character '\u0404' in position
    2: character maps to <undefined>
    

    Note that print li “works” on Python 2.x only because repr() is called implicitly. In general one should just do print repr(thing). Note also that print(li) can fail on Python 3.x because it implicitly calls repr(), not ascii()

    Update 2 If you want to find all non-strings in a list, do it explicitly, don’t rely on “tricks”:

    >>> def check_list(li):
    ...     for x, v in enumerate(li):
    ...         if not isinstance(v, (str, unicode)):
    ...             print "Type %s (%r) at offset %d" % (type(v), v, x)
    ...
    >>> check_list(['\xff', 2, u'\u0303', 4.0])
    Type <type 'int'> (2) at offset 1
    Type <type 'float'> (4.0) at offset 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came accross this html snippet & to my surprise jQuery Object does get
how i do validate two date textboxes using jquery. i came across this snippet
I'm playing around with list comprehensions and I came across this little snippet on
I came accross this html multiple file upload tutorial: http://robertnyman.com/2010/12/16/utilizing-the-html5-file-api-to-choose-upload-preview-and-see-progress-for-multiple-files/ I'm new to web
I came accross this code on the web: is_char(Ch) -> if Ch < 0
I came accross this on the Mike Ash Care and feeding of singletons and
I'm working on a web app and I came across this code snippit $email=$_POST['email'];
I came accross this: t = Clamp(t/d, 0, 1) but I'm not sure how
I was playing around with javascript prototype chain inheritance and i came accross this
While trying to understand how a web server worked, I came accross this: //myfile.js

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.