Python for loop items in single array

Question:

How can I convert incoming for loop items into a single array?

for txn in txn_card:
    text = txn.text
    text = np.array([text])
    print(text)

Desired output:
[[item1] ,[item2] ,[item3]]

Asked By: Vinod R

||

Answers:

With your ‘comment’, you may try something along the line of list comprehension

## define 'items'
#txn_card = ['item1', 'item2', 'item3']
##or
txn_card = [['item1'], ['item2'], ['item3']]

## Use a list comprehension to create the new array
#new_txn_card = np.array([item for item in txn_card])
new_txn_card = [item for item in txn_card]

## Print the new array
print(new_txn_card)
Answered By: semmyk-research
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.