-1

Here is my code to combine the three lists.

one=['nlo_90', 'nhi_76', 'nhi_88']
two=['12', '44', '84']
three=[['a','a','b','c'], ['g','a','g','g'], ['b','g','g','b']]
new_three=[list(dict.fromkeys(q)) for q in three]

z=zip(one,two,new_three)
for a,b,c in z:
    print(f'a:{a},\tb:{b},\tc:{c}')

Below is the output:

a:nlo_90,   b:12,   c:['a', 'b', 'c']
a:nhi_76,   b:44,   c:['g', 'a']
a:nhi_88,   b:84,   c:['b', 'g']

My desired output is:

a:nlo_90,   b:12,   c:a, b, c
a:nhi_76,   b:44,   c:g, a
a:nhi_88,   b:84,   c:b, g
LightCC
  • 9,804
  • 5
  • 52
  • 92
qwe
  • 27
  • 4
  • 1
    ``", ".join(c)`` ? – sushanth Aug 06 '20 at 03:31
  • Not what you asked for, but consider using `set` instead of your dict method for removing duplicates from `three`. `new_three = [set(q) for q in three]`. – chthonicdaemon Aug 06 '20 at 03:49
  • Building strings from component parts is available from any tutorial on strings. We expect you to do basic research before posting here. Where did you get stuck with those materials? – Prune Aug 06 '20 at 04:00

1 Answers1

0

Just convert the list into a joined string with ', ' as the internal separator. Currently, it's printing the full list: ['a', 'b', 'c'].

Using ', '.join(c) will take each item in the list, and put the ', ' between them as a combined string. Since your items are strings, there isn't any conversion during the combining, so final output is: a, b, c.

for a, b, c in z:
    c_str = ', '.join(c)
    print(f'a:{a},\tb:{b},\tc:{c_str}')
LightCC
  • 9,804
  • 5
  • 52
  • 92