I'm trying to put in some code that will delete a list from a list. For example if the user decides to delete the first names he would type in 'first' (w/out quotes). however after they input the name nothing prints out. Any suggestions?

person = ' '  
first = ['robert', 'jim', 'jack']
last = ['smith', 'white', 'black']
peeps = [first, last]      
pName = raw_input("Do you wish to delete 'first' or 'last' ?: ")
for person in peeps:
    if person == "%s" % (pName):
        pName = person
        print person
        peeps.remove(person)
        print peeps

because the if statement will never equate to true person is a list and pname is a string.

Member Avatar for masterofpuppets

try this :

person = ""
first = [ "robert", "jim", "jack" ]
last = [ "smith", "white", "black" ]
peeps = [ first, last ]      
pName = raw_input( "Do you wish to delete 'first' or 'last' ?: " )

if pName == "first":
   del peeps[ 0 ]
elif pName == "last":
   del peeps[ 1 ]
else:
   print "some bad input message or wtv."

print peeps

your mistake is here

if person == "%s" % (pName):

when you select items from lists you need to index them using integers and when you add a variable to a list the value of the variable is added, not the name itself. Hope that helps

I'm trying to put in some code that will delete a list from a list. For example if the user decides to delete the first names he would type in 'first' (w/out quotes). however after they input the name nothing prints out. Any suggestions?

person = ' '  
first = ['robert', 'jim', 'jack']
last = ['smith', 'white', 'black']
peeps = [first, last]      
pName = raw_input("Do you wish to delete 'first' or 'last' ?: ")
for person in peeps:
    if person == "%s" % (pName):
        pName = person
        print person
        peeps.remove(person)
        print peeps

A little confusing, from your code it looks like you want to remove a person's name, rather than a whole nested list.

Thanks for the help everyone (:

Now my next question. I have it so the users can input lists inside of the lists (Is that the best way to describe it) So say a user creates the following lists and adds it to the peeps list.

middle = ['a', 'b', 'c']
prefix = ['mr', 'ms', 'mrs', 'sir']
suffix = ['jr', 'snr', 'III']

how would I go about finding the index number for whatever list that they added so that I may delete it?

Thanks for all the help

IDLE 2.6.2      
>>> first = [ "robert", "jim", "jack" ]
>>> last = [ "smith", "white", "black" ]
>>> peeps = [ first, last ]
>>> peeps
[['robert', 'jim', 'jack'], ['smith', 'white', 'black']]
>>> prefix = ['mr', 'ms', 'mrs', 'sir']
>>> peeps.append(prefix)
>>> peeps
[['robert', 'jim', 'jack'], ['smith', 'white', 'black'], ['mr', 'ms', 'mrs', 'sir']]
>>> #append put list last

>>> peeps = [ first, last ]
>>> peeps
[['robert', 'jim', 'jack'], ['smith', 'white', 'black']]
>>> peeps.insert(1, prefix)
>>> peeps
[['robert', 'jim', 'jack'], ['mr', 'ms', 'mrs', 'sir'], ['smith', 'white', 'black']]
>>> #insert(i, list)here can you control placement

>>> #so now you can remove the insert list at placement 1
>>> peeps.pop(1)
['mr', 'ms', 'mrs', 'sir']
>>> peeps
[['robert', 'jim', 'jack'], ['smith', 'white', 'black']]

eh...I'm probably complicating things so let me just post my code.

def main():
    """
    main function
    """
    stock1 = ["Google","GOOG", 100, 24.00, 21.98]
    stock2 = ["Microsoft", "MCST", 300, 56.00, 54.00]
    stock3 = ["Yahoo", "YHOO", 90, 20.00, 14.00]
    stock4 = ["Apple", "AAPL", 289, 50.00, 46.99]
    
    portfolio = [stock1, stock2, stock3, stock4]
    ticker = [word [1] for word in portfolio]
    name = [word[0] for word in portfolio]
    
    while True: 
        stChoice = int(raw_input("What would you like to do?  \n Press 1 to add stock\n Press 2 to remove stock\n Press 3 to view stocks \n"))
        if stChoice == 1:
        
            cName = raw_input("Enter the company name: ")
            cTicker = raw_input("Enter the company ticker: ")
            cShare = raw_input("Enter the amount of shares: ")
            cPrice = raw_input ("Enter the price: ")
            cWorth = raw_input ("Enter the worth: ")
            stName = [cName,cTicker,cShare,cPrice,cWorth]
            stName = cTicker
            cTicker = [cName,cTicker,cShare,cPrice,cWorth]
                
            print cTicker
            portfolio.append (cTicker)
            print portfolio
        """ 
        ignore for now
        elif stChoice == 2:
            print name
            rmv = raw_input("Type in the ticker name for the stock you wish to remove. \n %s: " % ticker)
            portfolio.remove("%s") % rmv
            print portfolio
        """
    
            
        if stChoice == 2:
       
            ticker = [word [1] for word in portfolio]
            name = [word[0] for word in portfolio]
            person = ''
            print name
            print person
            selection = raw_input("Type in the ticker name of the stock that you would like to delete. \n %s:" % ticker)
         


                    
if __name__ == '__main__':
	main()

I know how to delete the first four stocks but how would I delete any that the user may have entered in?

This will get you started, but a dictionary would work much better for this.

elif stChoice == 2:
    for stock_list in portfolio:
        print "symbol = %s for %s" % (stock_list[1], stock_list[0])

    rmv = raw_input("Type in the ticker name for the stock you wish to remove. ")
    for stock_list in portfolio:
        ticker = stock_list[1]
        if ticker == rmv:
            portfolio.remove(stock_list)
    print portfolio

Thanks for the help. That did exactly what I was trying to do and now I know how to do it and why before I couldn't do it.

Though I do have yet another question. Would somebody explain to me the stock_list and how it's working. I'm kind of confused because stock_list wasn't defined and yeah... (sorry, new at python)

Why/how would a dictionary be better? We skimmed on dictionaries so I don't know to much about them but I would like to know more.

Also any suggestions for python sites besides pythons official one? I find myself looking on sites for information rather than looking in my book (Which even the teacher hates and regrets getting)

Dictionaries are indexed via a hash and so make it easy to look up things, in this case the stock symbol, without going through the sub-strings in each list. Also, they provide a convenient way to validate the data entered. In this example, "while rmv not in pf_dict:" will keep asking for an entry until one is entered that is in the dictionary. A simple example using the stock symbol as key, and a list with all of the other data as the item associated with the key.

stock1 = ["Google","GOOG", 100, 24.00, 21.98]
stock2 = ["Microsoft", "MCST", 300, 56.00, 54.00]
stock3 = ["Yahoo", "YHOO", 90, 20.00, 14.00]
stock4 = ["Apple", "AAPL", 289, 50.00, 46.99]

## assume the four lists were entered into a dictionary instead
pf_dict = {}
## indexed on symbol
for stock in [stock1, stock2, stock3, stock4 ]:
    pf_dict[stock[1]] = [stock[0], stock[2], stock[3]]
    print stock[1], pf_dict[stock[1]]

stChoice = 2
if stChoice == 2:
    rmv = ""
    while rmv not in pf_dict:
        print
        for key in pf_dict.keys():
            print "symbol = %s for %s" % (key, pf_dict[key][0])

        rmv = raw_input("Type in the ticker name for the stock you wish to remove. ")
        rmv = rmv.upper()
        if rmv not in pf_dict:
            print rmv, "is not a valid symbol"

    ##   valid symbol entered
    del pf_dict[rmv]
    print "\n", rmv, "deleted\n"
    print pf_dict

Would somebody explain to me the stock_list and how it's working. I'm kind of confused because stock_list wasn't defined

for stock_list in portfolio:
        print "stock_list =", stock_list
#
for n in range(0, 6):
    print n
#
x = range(0, 6)
print type(x), x
print type(portfolio), portfolio

A hint, stock_list is just a pointer to another (already defined) block in memory, not a separate block of memory.

Member Avatar for masterofpuppets

here's an example using dictionaries:

>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}
>>> tel.keys()
['guido', 'irv', 'jack']
>>>tel.values()
[ 4127, 4127, 4098 ]
>>> 'guido' in tel
True

for more data structure info go to http://docs.python.org/tutorial/datastructures.html

A little late, but this is where stock_list comes from ...

# data from OP code ...
stock1 = ["Google","GOOG", 100, 24.00, 21.98]
stock2 = ["Microsoft", "MSFT", 300, 56.00, 54.00]
stock3 = ["Yahoo", "YHOO", 90, 20.00, 14.00]
stock4 = ["Apple", "AAPL", 289, 50.00, 46.99]

portfolio = [stock1, stock2, stock3, stock4]

# go through the stock lists in the portfolio
for stock_list in portfolio:
    print stock_list
    print stock_list[0], stock_list[1], stock_list[2]

"""my result -->
['Google', 'GOOG', 100, 24.0, 21.98]
Google GOOG 100
['Microsoft', 'MSFT', 300, 56.0, 54.0]
Microsoft MSFT 300
['Yahoo', 'YHOO', 90, 20.0, 14.0]
Yahoo YHOO 90
['Apple', 'AAPL', 289, 50.0, 46.990000000000002]
Apple AAPL 289
"""

Should look familiar to you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.