'how to change list presentation

How can I change my list in a Bigger example

From:

b = rdd_two.rdd.map(list)
for new_rrd_list in b.collect():
   print(new_rrd_list)  

output look like this:

['lunar', 2.862386971953217] 
['satellite', 2.7074850119674734] 
['needs', 3.7074850119674734]  
['fuel', 3.230363757247811]  
['regular', 4.008515007631455]  
['orbit', 2.646787171613862]  
['corrections', 3.5313937529117925]

To:

print(wanted_list)

output:

    [{'lunar': 2.862386971953217, 
      'satellite': 2.7074850119674734,
      'needs': 3.7074850119674734,
      'fuel': 3.230363757247811,
      'regular': 4.008515007631455,
      'orbit': 2.646787171613862,
      'corrections': 3.5313937529117925}
    ]


Solution 1:[1]

This loop doesn't give you a single list, it iterates over everything in b.collect() and produces a sequence of different lists:

for new_rrd_list in b.collect():
   print(new_rrd_list) 

To get your wanted_list (a list containing a single dictionary whose keys and values come from pairs yielded by b.collect()), you want to instead turn that iterable into a dict, and then put that dict in a list:

wanted_list = [dict(b.collect())]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Samwise