42685

How to use one print() in python to print list items line by line - not in one streak?

Question:

mister is a list of lists. print(mister) gives this in the Python shell:

[['ququ.kz', 1], ['gp.kz', 1], ['gmail.ru', 1], ['mail.ru', 1], ['tlc.com', 1], ['mail.ko', 1], ['microsoft.jp', 1], ['hotmail.eu', 1], ['soman.com', 1], ['swedenborgen.sn', 1], ['customergoogle.com', 1], ['mail.jp', 2], ['gmail.com', 3], ['mail.ru', 3], ['hotmail.com', 3], ['mail.jp', 3], ['mail.com', 4], ['hotmail.com', 4], ['gmail.com', 4], ['mail.kz', 5], ['mail.cn', 7], ['hotmail.com', 9], ['customers.kz', 9], ['microsoft.com', 10], ['conestogamall.com', 13]]

Can I use print() one time and get nice output - line by line, instead of one big streak? Or is the only solution:

for email_date_entry in mister: print(email_date_entry)

Is there another elegant way to use <strong>only one print call</strong>?

Answer1:

You can pass your list as <em>separate</em> arguments to print() using the * variable argument syntax:

print(*mister, sep='\n')

Now each element in mister is seen as a separate argument, and is printed with a \n separator:

>>> mister = [['ququ.kz', 1], ['gp.kz', 1], ['gmail.ru', 1], ['mail.ru', 1], ['tlc.com', 1], ['mail.ko', 1], ['microsoft.jp', 1], ['hotmail.eu', 1], ['soman.com', 1], ['swedenborgen.sn', 1], ['customergoogle.com', 1], ['mail.jp', 2], ['gmail.com', 3], ['mail.ru', 3], ['hotmail.com', 3], ['mail.jp', 3], ['mail.com', 4], ['hotmail.com', 4], ['gmail.com', 4], ['mail.kz', 5], ['mail.cn', 7], ['hotmail.com', 9], ['customers.kz', 9], ['microsoft.com', 10], ['conestogamall.com', 13]] >>> print(*mister, sep='\n') ['ququ.kz', 1] ['gp.kz', 1] ['gmail.ru', 1] ['mail.ru', 1] ['tlc.com', 1] ['mail.ko', 1] ['microsoft.jp', 1] ['hotmail.eu', 1] ['soman.com', 1] ['swedenborgen.sn', 1] ['customergoogle.com', 1] ['mail.jp', 2] ['gmail.com', 3] ['mail.ru', 3] ['hotmail.com', 3] ['mail.jp', 3] ['mail.com', 4] ['hotmail.com', 4] ['gmail.com', 4] ['mail.kz', 5] ['mail.cn', 7] ['hotmail.com', 9] ['customers.kz', 9] ['microsoft.com', 10] ['conestogamall.com', 13]

Answer2:

You can also use pprint (Pretty Print) from pprint module - it works for almost every type and usually gives nice output. Usage:

from pprint import pprint ... pprint(mister)

Answer3:

<strong>Edit based on @jonrsharpe's suggestion</strong>

Try this:

print('\n'.join(map(str, mister)))

What this code does:

<ul><li>Converts to string every item in the list.</li> <li>Joins all obtained strings by a line break separator.</li> <li>Prints the result.</li> </ul>

This approach works for both Python2.x and Python3.x.

Recommend

  • Write a MERGE Statement for mis-matching values
  • VBA Directory Referencing
  • How many .snk files do I need?
  • Applying semicontext notation for passing additional arguments
  • Pending Intent triggered by Alarm Manager seems to fire right away
  • add an element to a node, if it doesnot exist
  • Magento “Please specify the product's option(s)” message remains after completing add to cart a
  • WPF Listbox commands
  • Neo4j: Filter nodes based on aggregate function
  • Zend Framework bassed projects
  • ZipList with Scalaz
  • How do I superscript characters in a UIButton?
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • onBackPressed() not being executed
  • Passing 1 of 3 values to a Sub
  • debug library loaded with ctypes using gdb
  • How to install a .deb file on a jailbroken iphone programmatically?
  • How to use an array of arrays with array_map(…) in PHP?
  • How to rebase a series of branches?
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Ajax jQuery multiple calls at the same time - long wait for answer and not able to cancel
  • What is Eclipse's Declaration View used for?
  • Is there a amazon webstore API for customers?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Perl system calls when running as another user using sudo
  • Deserializing XML into class C#
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Adding custom controls to a full screen movie
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • SVN: Merging two branches together
  • Load html files in TinyMce
  • Change div Background jquery
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How does Linux kernel interrupt the application?
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Busy indicator not showing up in wpf window [duplicate]
  • How to Embed XSL into XML
  • Why do underscore prefixed variables exist?