stack = list()
stack = [[u'hello','world'],[u'blue','sky']]
How to print 'hello world' separately and 'blue sky' separately in python?
Use pprint, this will work for any size and nesting of array.
>>> import pprint
>>> stack = list()
>>> stack = [[u'hello','world'],[u'blue','sky']]
>>> pprint.pprint(stack)
[[u'hello', 'world'], [u'blue', 'sky']]
>>>
Specifically use this
for s in stack:
print ' '.join(s)
Try this way:
print stack[0][0]+' '+stack[0][1]
More: Consider this piece of code this way, I print certain object (OK, it's an unicode object)combined with 3 parts, the first part is the object from a list object,and the list object comes from stack (which is also a list object). It's like this: list(stack)->list(stack[0])->unicode(u'hello') The second part is a string object: ' '(a space) The third part is just like the first part, list(stack)->list(stack[0])->str('world') Put these 3 parts together comes the result you have seen.
I suggest you think about exactly what the types of the THINGS you are using are. Because for everything in python, if you know the type of it, you most likely will know what built-in functions/methods/operators you can use.This could be great!
And one more thing, I print a unicode object together with 2 str objects.
The idea is to convert each list to string by str.join() before printing.
>>> stack = [[u'hello', 'world'], [u'blue','sky']]
>>> print '\n'.join( ' '.join(s) for s in stack )
hello world
blue sky
Using loops:
>>> stack = [[u'hello', 'world'], [u'blue','sky']]
>>> for s in stack:
... print ' '.join(s)
...
hello world
blue sky
If you want to modify the list:
>>> stack = [[u'hello', 'world'], [u'blue','sky']]
>>> stack = [ ' '.join(s) for s in stack ]
>>> print '\n'.join( s for s in stack )
hello world
blue sky