3
l = [['John'],['rides'],['bike'],['with'],['Mr.','Brown'],]

Assume that i have a list object like that. How can print my list objects in a row without using for statement and append to a string ?

My desired output:

print("my sentence:" + ? )
print("people:" + ? + ", " + ?)

my sentence: John rides bike with Mr. Brown
people: John, Mr. Brown

MAJOR EDIT:

The reason why i create this question is i analyze text based documentation and extract information with named entity so my problem is when i grouo sequential proper nouns which are "name + surname" or " Blah Blah Organization" in list like that :

[[u'Milletvekili', u'Ay\u015fe'], [u'oraya'], [u'Merve', u'Han\u0131m'], [u'ile'], [u'gitti'], [u'.']]

I have an algorithm which compare list objects with my datasets and describes the entity type. After the if statement decided [u'Milletvekili', u'Ay\u015fe'] is a person name ;

if gettype(arr[i][0].split("'")[0]) == "Özel İsim":
      newarr.append("[Person: " + str(arr[i][0]) + "]")

i append that in my new list which will be my in my output like [Person: Mr. Brown]. But i must group Capitalized words then i always have my output like that:

 [Person: [u'Milletvekili', u'Ay\u015fe']] 
Arda Nalbant
  • 479
  • 2
  • 7
  • 16

3 Answers3

4

Not quite elegant as the "sum", but alternatively you can use str.join() and map() with functools.partial():

>>> from functools import partial
>>> " ".join(map(partial(str.join, " "), l))
'John rides bike with Mr. Brown'
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • this also what im looking for but output is [Person: [u'Mr.', u'Brown']] any idea why i cant print objects in a row ? – Arda Nalbant May 01 '16 at 17:12
  • 1
    @ArdaNalbant sorry, difficult to say. It is not clear what input data you've got at the moment and what is your desired output..could you edit the question and clarify? Thanks so much. – alecxe May 01 '16 at 18:53
  • Edited my question and im afraid its not clear to understand i think but all i can say is i get [Person: [u'Mr.', u'Brown']] instead of [Person: Mr.Brown] – Arda Nalbant May 01 '16 at 19:53
4

Borrowing from this answer:

" ".join(sum(l, []))
Out[130]: 'John rides bike with Mr. Brown'
Community
  • 1
  • 1
ayhan
  • 70,170
  • 20
  • 182
  • 203
3

Flatten the list, then join with ' '.

>>> ' '.join(s for sub in l for s in sub)
'John rides bike with Mr. Brown'

Or with itertools.chain:

>>> from itertools import chain
>>> ' '.join(chain(*l))
'John rides bike with Mr. Brown'
timgeb
  • 76,762
  • 20
  • 123
  • 145