-3

i'm trying to create an empty nested list in python. I have searched for information regarding nested list and most are usually nested lists that already has values in it.

The reason I needed a nested list is because I have a set of items: Apple, Pears and Oranges. However, Each item has another list that consists of: ID, price, quantity.

So what I'm planning is to: 1. Put all items into index 0,1,2 respectively. So, apple = [0], pears =[1], oranges =[2] 2. Create another nested list to put ID,price and quantity. So using apple as an example, I would do: apple[0][0].append(ID), apple[0][1].append(price), apple[0][2].append(quantity)

Is it possible for nested list to work these way? Or is there any other way to create an empty nested list? By the way, the apple, pears and orange all have their own python code file, so I have to import the pears and orange python file into the apple python file. I would appreciate any help given. Thank You.

Codes from apple.py:

fruits = [],[]

fruits[0],[0].append("ID")
fruits[0],[1].append("price")
fruits[0],[2].append("quantity")

Codes from pears.py:

fruits = [],[]

fruits[1],[0].append("ID")
fruits[1],[1].append("price")
fruits[1],[2].append("quantity")
JasonSmith
  • 27
  • 1
  • 7
  • 1
    there is no nested list in your code....actually your code doesn't make any sense – Netwave Jun 02 '17 at 07:23
  • perhaps, you can show how empty nested list is created instead? – JasonSmith Jun 02 '17 at 07:33
  • this is not a make me software service. Try your homework first and study hard. This code spits no effort at all. – Netwave Jun 02 '17 at 07:34
  • cool, your comment actually made no relevance considering you just outright claimed my codes doesn't make any sense even though the people who answered below has actually used the same method that I did. Fyi, these ain't my whole codes. Ever heard of constructive criticism? Your comment is not a example of one. – JasonSmith Jun 02 '17 at 07:38

2 Answers2

1

Yes, you can do this, however be aware that your fruits object is a tuple, not a list. This means you can't append new types of fruit to it.

>>> fruits = [],[]
>>> type(fruits)
<type 'tuple'>

>>> fruits.append([])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'

Use additional square brackets to make it a list of lists:

>>> fruits = [[],[]]
>>> type(fruits)
<type 'list'>
>>> fruits.append([])
>>> fruits
[[], [], []]

Then you can populate the inner lists as you expect, only you need to omit that extra index and comma, which were not valid Python:

>>> fruits[0].append("ID")
>>> fruits[0].append("price")
>>> fruits[0].append("quantity")
>>> fruits
[['ID', 'price', 'quantity'], [], []]

And then you would repeat with fruit[1], fruit[2].

Or you can do them all in a loop:

fruits = []
for n in range(3):
    fruits[n].append("ID")
    fruits[n].append("price")
    fruits[n].append("quantity")

But really, this sounds like something you should be using a dict for:

fruits = {}
for id, fruit in enumerate(["apples", "pears", "oranges"]):
    fruits[fruit] = { "ID": id, "price": None, "quantity": 0 }

Then you can use them like this:

fruits["apples"]["price"] = 2.99
fruits["apples"]["quantity"] = 5
David Scarlett
  • 3,171
  • 2
  • 12
  • 28
  • hello, thanks for the reply. Is it possible to create database using the dict method?? – JasonSmith Jun 02 '17 at 07:35
  • If you want to persist data, the [shelve](https://docs.python.org/3/library/shelve.html) module provides lightweight persistence of dictionaries. – David Scarlett Jun 02 '17 at 07:38
0

You can use python dictionary and do it this way:

fruits={}
one_fruit={'ID':0,'price':0,'quantity':0}
fruits['apple']=one_fruit

or you can do it with lists in this way:

fruits = [],[]    
fruits[0].append("ID")
fruits[0].append("price")
fruits[0].append("quantity")

I would choose the first method, with dictionaries, but depends of what you are going to do

nacho
  • 5,280
  • 2
  • 25
  • 34
  • hello, thanks for the reply. For the 'one_fruit={'ID':0,'price':0,'quantity':0}', is it possible if i use a variable instead of hard coding the values? This is because the ID is randomly generated and added to the list in a while loop – JasonSmith Jun 02 '17 at 07:30
  • Yes, of course it is possible to use a variable like 'one_fruit={'ID':variableID,'price':variable_price , 'quantity':variable_quantity}. You even can do something like 'fruits['apple']['ID']=variable_ID' – nacho Jun 02 '17 at 07:34
  • @JasonSmith And you could vote the answer and the help, so next time wi´ll be glad to do it – nacho Jun 02 '17 at 19:29