0

Originally this was lists within a list:

print results

[['aaa664847', 'Completed', 'location', 'mode', '2014-xx-ddT20:00:00.000']
['aaa665487', 'Completed', 'location', 'mode', '2014-xx-ddT19:00:00.000']
['aaa661965', 'Completed', 'location', 'mode', '2014-xx-ddT18:00:00.000']]

However, I needed to join the elements within the nested list which then prints out like this:

print results1

['aaa664847, Completed, location, mode, 2014-xx-ddT20:00:00.000']
['aaa665487, Completed, location, mode, 2014-xx-ddT19:00:00.000']
['aaa661965, Completed, location, mode, 2014-xx-ddT18:00:00.000']

I need to get the results back into a list within a list:

[['aaa664847, Completed, location, mode, 2014-xxddT20:00:00.000'],
['aaa665487, Completed, location, mode, 2014-xx-ddT19:00:00.000']]
4
  • "I needed to join the elements within the nested list which then prints out like this" -- how did you do this? What is the type of results1? Commented Mar 7, 2015 at 23:54
  • @jedwards results1 is probably a list as well and he forgot outermost brackets Commented Mar 7, 2015 at 23:57
  • i used a join and then appended it to results1 Commented Mar 8, 2015 at 0:05
  • @S_McC See my answer below. It has the desired effect you wish to implement. Commented Mar 8, 2015 at 0:05

2 Answers 2

2

It's not really clear whether you're trying to get from results to results1 or from results1 to new_Result (i.e. back to results).

But consider:

import pprint

results = [
['aaa664847', 'Completed', 'location', 'mode', '2014-xx-ddT20:00:00.000'],
['aaa665487', 'Completed', 'location', 'mode', '2014-xx-ddT19:00:00.000'],
['aaa661965', 'Completed', 'location', 'mode', '2014-xx-ddT18:00:00.000']
]

# Go from results -> results1
results1 = [', '.join(x) for x in results]
pprint.pprint(results1)
#  ['aaa664847, Completed, location, mode, 2014-xx-ddT20:00:00.000',
#   'aaa665487, Completed, location, mode, 2014-xx-ddT19:00:00.000',
#   'aaa661965, Completed, location, mode, 2014-xx-ddT18:00:00.000']    


# Go from results1 to new_Result
new_Result = [x.split(', ') for x in results1]
pprint.pprint(new_Result)
#  [['aaa664847', 'Completed', 'location', 'mode', '2014-xx-ddT20:00:00.000'],
#   ['aaa665487', 'Completed', 'location', 'mode', '2014-xx-ddT19:00:00.000'],
#   ['aaa661965', 'Completed', 'location', 'mode', '2014-xx-ddT18:00:00.000']]

print results == new_Result    # True
Sign up to request clarification or add additional context in comments.

Comments

0

You mean something like this:

your_list = [[', '.join(i)] for i in your_list]

Now your list is equivalent to:

[['aaa664847, Completed, location, mode, 2014-xx-ddT20:00:00.000'], 
 ['aaa665487, Completed, location, mode, 2014-xx-ddT19:00:00.000'], 
 ['aaa661965, Completed, location, mode, 2014-xx-ddT18:00:00.000']]

To print these individually try:

for item in your_list:
    print item

# Output is as follows ...

['aaa664847, Completed, location, mode, 2014-xx-ddT20:00:00.000'] 
['aaa665487, Completed, location, mode, 2014-xx-ddT19:00:00.000'] 
['aaa661965, Completed, location, mode, 2014-xx-ddT18:00:00.000']

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.