0

Is there a way to separate list items with \n sequence? here's the code:

month_names = [
                ['January', 'February', 'March'],
                ['April', 'May', 'June'],
                ['July', 'August', 'September'],
                ['October', 'November', 'December'],
                ]   
print month_names 

the problem is when I try to add \n or '\n' to the list or print like :

print month_names + '\n'

or

['January', 'February', 'March'], '\n',

syntax or type error raises and sometime it prints \n.

The Desired Output is :

'January', 'February', 'March'
'April', 'May', 'June'
'July', 'August', 'September'
'October', 'November', 'December'

and I want to keep it as a list so I can recall the 2nd list like:

'April', 'May', 'June' 
9
  • 1
    Why do you want it? What is it that you are trying to achieve? Seems like an XY problem to me . Can you show what you are expecting as the output? Commented Aug 9, 2015 at 7:04
  • @AnandSKumar Trying to define a list which contains: Spring (Jan, Feb, Mar) Summer (Apr, May, June) and so one. and when I recall whole the list it prints whole of it. Commented Aug 9, 2015 at 7:11
  • What is your desired/expected output? Please share. Commented Aug 9, 2015 at 7:11
  • @closevoters: I see no where OP's aversion in using join. Commented Aug 9, 2015 at 7:12
  • 1
    @Heartagramir Please update the question with what else you are expecting as output Commented Aug 9, 2015 at 7:18

1 Answer 1

3

I believe what you want is a simple concatenation of individual months with new line

>>> print '\n'.join(map('\n'.join, month_names))
January
February
March
April
May
June
July
August
September
October
November
December

In case, you want quarters in each line, one of the following should work

>>> print '\n'.join(map(' '.join, month_names))
January February March
April May June
July August September
October November December

>>> print '\n'.join(map(str, month_names))
['January', 'February', 'March']
['April', 'May', 'June']
['July', 'August', 'September']
['October', 'November', 'December']
Sign up to request clarification or add additional context in comments.

2 Comments

print '\n'.join(map(' '.join, month_names)) is what I've expected.
@Heartagramir If the answer was helpful for you, you should accept it (by clicking on the tick mark on the left side) .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.