0
list=[['name1', 'maths,english'], ['name2', 'maths,science']]

I have a nested list that likes something like this, I am trying to work out how to format it so that the out put would be something like the following:

name1, maths,english
name2, maths,science

I have tried using regex to no avail. How would I go about formatting or manipulating the list output to get something like the above?

3
  • Possible duplicate of Formatting nested lists/tuples Commented Oct 12, 2016 at 19:57
  • Why would regex work? It's for parsing strings, not creating them. Commented Oct 12, 2016 at 20:04
  • @jonrsharpe I was treating the list as a string, I see now Commented Oct 12, 2016 at 20:05

2 Answers 2

5

Iterate over your groups and join the items from each group using a comma.

groups = [['name1', 'maths,english'], ['name2', 'maths,science']]

for group in groups:
    print ', '.join(group)
Sign up to request clarification or add additional context in comments.

3 Comments

Note: unlike the original post, Soviut uses groups as the name for the variable, since list is a standard global function in python. It's usually a good idea to avoid clashes with global names
The output I'm getting from this is is character on a single line
I apologise, I fixed my issue above by removing the str() function when getting my output for the list, thanks for the solution greatly appreciated
0

In Python3 you could use format() as described in PEP 3101


fmt_groups = "\n".join(["{},{}".format(*group) for group in groups])
print(fmt_groups)

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.