2

Let's take:

my_list=[["a","b","c"],["d","e","f"],["g","h","i"],["j","k","l"]]

And the result I'm looking for:

0. a    _ b    (c)
1. d    _ e    (f)
2. g    _ h    (j)
3. j    _ k    (l)
4
  • Do you want to get each element of your list? like [a,b,c] or only [a]? Commented Dec 5, 2018 at 15:32
  • 1
    My understanding of your question as it is given is that for each sub-list, you want to print a number, first element, underscore, second element and finally third element in parentheses. Is that correct? Commented Dec 5, 2018 at 15:34
  • It is not clear what you are asking for; the expected output isn't in any Python structure and you didn't specify the rules to have the expected output Commented Dec 5, 2018 at 15:35
  • yes, it is exactly what i want Commented Dec 5, 2018 at 16:10

4 Answers 4

5

To get exactly your output printed in the console, iterate over the outer list and use enumerate and str.format:

values = [["a","b","c"],["d","e","f"],["g","h","i"],["j","k","l"]]

for i, x in enumerate(values):
    print("{}. {}    _ {}    ({})".format(i, *x))
# 0. a    _ b    (c)
# 1. d    _ e    (f)
# 2. g    _ h    (i)
# 3. j    _ k    (l)
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming a,b,c... are integers

A = [[8, 7, 2], [1, 4, 12], [6, 5, 4]]

B = "\n".join(["%d_%d(%d)" % tuple(a) for a in A])

print(B)

if these are strings (not very clear in question), just use %s instead of %d

Comments

0

Other option:

lst = [['a','b','c'],['d','e','f'],['g','h','i'],['j','k','l']]

lines = [x[0]+'_'+x[1]+' ('+x[2]+')' for x in lst]
for i, line in enumerate(lines):
  print( str(i+1) + '. ' + line)

It returns:

# 1. a_b (c)
# 2. d_e (f)
# 3. g_h (i)
# 4. j_k (l)

Comments

0

Try this one:

print '\n'.join(str(n) +'. '+e[0] + ' _'+ e[1] + '  ('+e[2]+')' for n,e in enumerate(my_list))

You should have:

0. a _b  (c)
1. d _e  (f)
2. g _h  (i)
3. j _k  (l)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.