Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending item in multiple dimensions list

Tags:

python

I am not sure what this thing I am trying to do is called in Python, but how do I add items to a multi-dimension list? I know how to do it to a regular list, and I tried to figure out this array stuff but completely getting lost.

I want to make the list like this

portfolio_list =[
['TSLA',5000], 
['BA',2000], 
['MSFT',2000], 
['AAPL',1500],
]

I have this code in a while loop

new_item = input("Add stock ticker > ").upper()
add_to_list(new_item) #adds stock ticker to list 

new_value = input("What is value of {} > ".format(new_item))
add_item_value(new_value)

and the definitions I made for these are

def add_to_list(item): 
    portfolio_list.append([[item]],axis=0)
    print("{} has been added".format(item,))

def add_item_value(item):
    portfolio_list.append([[item]],axis=1)
    print("{} value has been added to {} ".format(item,new_item))

the error I am getting

Add stock ticker > tsla


Traceback (most recent call last):
  File "test_effecient_frontier.py", line 139, in <module>
    add_to_list(new_item) #adds stock ticker to list
  File "test_effecient_frontier.py", line 70, in add_to_list
    portfolio_list.append([[item]],axis=0)
TypeError: append() takes no keyword arguments

Any help is much appreciated!

like image 492
Jakub Avatar asked Dec 06 '25 08:12

Jakub


1 Answers

You may

  1. ask for both information
  2. append the new sublist to the main list, dont forget to convert to int
portfolio_list = [
    ['TSLA', 5000],
    ['BA', 2000],
    ['MSFT', 2000],
    ['AAPL', 1500],
]

new_item = input("Add stock ticker > ").upper()
new_value = input("What is value of {} > ".format(new_item))
portfolio_list.append([new_item, int(new_value)])

With a while loop, you could have somethink like

while True:
    new_item = input("Add stock ticker > ").upper()
    if new_item == "STOP":
        break
    new_value = input("What is value of {} > ".format(new_item))
    portfolio_list.append([new_item, int(new_value)])
like image 178
azro Avatar answered Dec 07 '25 21:12

azro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!