Greg
Greg

Reputation: 35

python merge lists using join()

I have got the following list:

lst = [['a','!b'], ['91','q'], ['!t','3','p']]

I want the following as output:

test = a and !b or 91 and q or !t and 3 and p

The individual literals in the sublist is made into a single literal and joined using 'and' and then the sublists are joined together using 'or'

I tried the following, can someone correct my code?

   def output(self):
      temp = ['and'.join(sublist[i]) for i in sublist]
      test = ['or'.join(sublist) for sublist in self.lst]
      return self.test

Upvotes: 1

Views: 64

Answers (1)

dlask
dlask

Reputation: 8982

All at once:

def output(lst):
    return " or ".join(" and ".join(s) for s in lst)

Upvotes: 5

Related Questions