Formatting Numpy list

Question:

Good Morning, im relatively new in python and I need some help with formatting. I have a Numpy list with the following format:

[['SPY'], ['XLE'], ['XLF'], ['XLY'], ['GLD'], ['FXE']]

But I would like to convert it to a list like this:

['Games', 'Fin']

Sorry for the dumb question and thank you for your help 🙂

Answers:

You can use the flatten method of the np.array.

import numpy as np

a = np.array([['SPY'], ['XLE'], ['XLF'], ['XLY'], ['GLD'], ['FXE']])

print(a.flatten())
# ['SPY' 'XLE' 'XLF' 'XLY' 'GLD' 'FXE']

Source: numpy docs

Answered By: Julien Sorin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.