Python Forum
How to list objects on separate lines?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to list objects on separate lines?
#1


Hello community,the issue seems simple but I just can't figure it out, so I'm going to get right to it. I saw it on the web before, there's a function to make each object in a list be printed out on a separate line. Like, if i ha a list "L" with objects "orange","blue" and "red", it could be delivered like this:

[python]

>>> Orange
blue
Red


Does any body know the code to make a list "print" out like this?
Reply
#2
https://docs.python.org/3/tutorial/index.html

There's a section on lists.
Reply
#3
>>> lst = ['Orange', 'Blue', 'Red']
>>> lst
['Orange', 'Blue', 'Red']
>>> # A loop work
>>> for item in lst:
...     print(item)
...     
Orange
Blue
Red

>>> # join a new line work
>>> '\n'.join(lst)
'Orange\nBlue\nRed'
>>> # print will show new line
>>> print('\n'.join(lst))
Orange
Blue
Red
Reply
#4
Thanks a lot, works perfect.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating list of lists, with objects from lists sgrinderud 7 1,610 Oct-01-2022, 07:15 PM
Last Post: Skaperen
Question Keyword to build list from list of objects? pfdjhfuys 3 1,555 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,361 May-17-2022, 07:49 AM
Last Post: Pedroski55
  How to store the resulting Doc objects into a list named A xinyulon 1 1,893 Mar-08-2022, 11:49 PM
Last Post: bowlofred
  Grouping and sum of a list of objects Otbredbaron 1 3,194 Oct-23-2021, 01:42 PM
Last Post: Gribouillis
  Passing List of Objects in Command Line Python usman 7 3,153 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,028 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,808 Aug-10-2020, 11:01 PM
Last Post: medatib531
  how do i get rid of next lines in my list() ironpotatoe58 10 3,987 Mar-31-2020, 03:57 AM
Last Post: SheeppOSU
  Organizing list of objects by attribute scarney1988 3 2,213 Mar-11-2020, 03:55 PM
Last Post: scarney1988

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020