Pythonic way to print list items
Assuming you are using Python 3.x:
print(*myList, sep='\n')
You can get the same behavior on Python 2.x using from __future__ import print_function
, as noted by mgilson in comments.
With the print statement on Python 2.x you will need iteration of some kind, regarding your question about print(p) for p in myList
not working, you can just use the following which does the same thing and is still one line:
for p in myList: print p
For a solution that uses '\n'.join()
, I prefer list comprehensions and generators over map()
so I would probably use the following:
print '\n'.join(str(p) for p in myList)
How to properly print a list?
In Python 2:
mylist = ['x', 3, 'b']
print '[%s]' % ', '.join(map(str, mylist))
In Python 3 (where print
is a builtin function and not a syntax feature anymore):
mylist = ['x', 3, 'b']
print('[%s]' % ', '.join(map(str, mylist)))
Both return:
[x, 3, b]
This is using the map()
function to call str for each element of mylist, creating a new list of strings that is then joined into one string with str.join()
. Then, the %
string formatting operator substitutes the string in instead of %s
in "[%s]"
.
Print list item in python
You can index into the list and dictionary -- accessing the first element using [0]
, and accessing the sole value of the dictionary using ['translation_text']
:
translated = pipe(reviews['review'][0])[0]['translation_text']
print list in proper way python
You have a list of type objects, and both __str__
and __repr__
of type objects have a <type 'x'>
form.
If you want to print list of names of type objects you need to perform conversion manually:
print [t.__name__ for t in values]
Related Topics
How to Cross Compile Python Interpreter for Windows Under Linux
Crontab Failed to Run Python Script at Reboot
Magicexception:File 5.41 Supports Only Version 16 Magic File, Magic.Mgc Is Version 14
Why Can't Python Sockets Resolve Url's with Http in It
Detect Face Then Autocrop Pictures
How to Get the Pythonpath in Shell
Serving a Request from Gunicorn
Error Installing Uwsgi in Virtualenv
Docker.Errors.Dockerexception: Error While Fetching Server API Version
Bring the Current Python Program to Background
Unix Socket Credential Passing in Python
To Read Line from File Without Getting "\N" Appended at the End
What Does "The Following Packages Will Be Superseded by a Higher Priority Channel" Mean
How to Use Expect on Windows Without Installing Cygwin
Can't Start Foreman in Heroku Tutorial Using Python