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]]
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)
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]]
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)