3

What is the wrong with my code:

l = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', 'h']
for i in l:
  for j in i:
    print(j)`

The output is:

a
bb
['ccc', 'ddd']
ee
ff
g
h.

But I want this output:

a
bb
ccc ddd
ee
ff
g
h.

What changes should be made?

Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
  • 2
    Test if `j` is a list. If it is, use `print(*j, sep=' ')` instead of `print(j)` – Barmar Jul 10 '21 at 06:49
  • Can you describe the rule you want to follow? What rule do you follow to decide to print `ccc` and `ddd` on the same line and everything else on its own line? – Matthias Fripp Jul 10 '21 at 07:13
  • Thanks for your concern. I just want to manipulate output as per my requirement. What if I want ccc and ddd on next line ? – M Suleman Tunio Jul 10 '21 at 07:20
  • A computer program can only follow rules that you give it. If you can't describe a rule that can be used to know that `ccc` and `ddd` should go on one line and everything else should go on its own line, then you are asking the computer (and us) to read your mind, which won't work. How can you tell which items should go on one line together and which should go on their own lines? – Matthias Fripp Jul 10 '21 at 07:31

5 Answers5

3

I'm assuming the rule is "print every item in list l or nested lists within l on its own line, unless a nested contains no sub-lists, in which case, print all its items on one line with spaces between."

If that's right, then this code should work:

l = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', 'h']
def print_list(lst):
    if not any(isinstance(x, list) for x in lst):
        # print list of strings on one line
        print(*lst)
        return
    # contains sublists; print or process items one by one
    for x in lst:
        if isinstance(x, list):
            print_list(x)  # process sub-list
        else:
            print(x)

print_list(l)
Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45
1

if you want to get all array values of different levels in the same level . in the bellow, an example can solve your problem

def find_value(list_):
    for i in list_:
        if isinstance(i,list):
            for j in find_value(i):
                yield j
        else:
            yield i
l = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', 'h']
for i in find_value(l):
    print(i)

may you give more information about what do you want?

yunus
  • 33
  • 1
  • 9
1

As @Barmar mentioned in the comments that j can be list. You can use isinstance to check if the variable if a specific type

l = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', 'h']
for i in l:
  for j in i:
      if isinstance(j,list):
          print(*j)
      else:
          print(j)
1

I'm assuming that you have 3-dimensional list.

l = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', 'h']
from functools import reduce

for first in l:
    for sec in first:
        if type(sec) == type(list()):
            iter = str(reduce(lambda x, y: str(x)+' '+str(y), sec))
            print(iter)
        else:
            print(sec)
amir ali
  • 111
  • 5
1
L = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', 'h']
k = 0
for i in L:
    for j in i:
        if k==2:
            for r in j:
                print(r)
                k =k+1
        else:
            print(j)
            k = k+1