Hi - I'm a rookie programmer. Could somebody explain how I can append a nested list of the form:

List =

[[Name1,[Detail-1,Counter-1],[Detail-2,Counter-2].....]
[Name2,[Detail-1,Counter-2],[Detail-2,Counter-2].....]
................................................. ]

My code is :

def program(List, Name, Detail):
for entry in List:
if entry[0] == Name:
entry[1].append([Detail,Counter])
return

List.append ([Name,[Detail]])
for entry in index:
if entry[0] == Name:
entry[1].append(Counter)

But this shows the List as:
[
[Name1,[Detail-1,Counter,[Detail-2,Counter-2],[Detail-3,Counter-3],.....]
[Name2,[Detail-1,Counter,[Detail-2,Counter-2],[Detail-3,Counter-3],.....]
]

So the problem is with the List index no. n[1][0] which means something is wrong with the highlighted (bold and italicised) block. Could you please look at it?

Any help is highly appreciated.

Thanks

Recommended Answers

All 3 Replies

Copied to preserve the code tags.

def program(List, Name, Detail):
    for entry in List:
        if entry[0] == Name:
            ## changed to add to the entry containing "Name" not 
            ## entry[1] as it is [Detail, Counter] so would become
            ## [Detail, Counter, [Detail_2, Counter_2]]
            entry.append([Detail,Counter])  ## "Counter" has not been declared
#-----------------------------------------------------------
#   return=You exit the function here if the name is found,
#   i.e. the following code is never reached and so is not
#   the problem
#-----------------------------------------------------------
            return
           
    List.append ([Name,[Detail]])
    for entry in index:           ## "index" is never declared
        if entry[0] == Name:
            entry[1].append(Counter)

Hi Woooee,

Thanks for your prompt response.

However, I still believe Line 7 has to be
"entry[1].append([Detail,Counter])",
because I want [Detail,Counter] as a nested list.

-Line 16 was my bad. It should read:


16. for entry in List:

I think the problem is in the block - Lines 15-18, because I'm getting the output of the List :

[
[Name1,[Detail-1,Counter-1,[Detail-2,Counter-2],[Detail-3,Counter-3],.....]
[Name2,[Detail-1,Counter-1,[Detail-2,Counter-2],[Detail-3,Counter-3],.....]
]

while I want it to be

[
[Name1,[Detail-1,Counter-1],[Detail-2,Counter-2],[Detail-3,Counter-3],.....]
[Name2,[Detail-1,Counter-1],[Detail-2,Counter-2],[Detail-3,Counter-3],.....]
]

1. I don't understand what Counter is, so a write 'Counter'.
2. I write you code (change only Counter to 'Counter')

>>> def program(List, Name, Detail):
...   for entry in List:
...     if entry[0] == Name:
...       entry.append([Detail,'Counter'])
...       return
...   List.append ([Name,[Detail]])
...   for entry in List:
...     if entry[0] == Name:
...       entry[1].append('Counter')

... and I got the correct result:

a = []
>>> program(a, 'Name1', 'Detail1')
>>> program(a, 'Name1', 'Detail2')
>>> program(a, 'Name2', 'Detail1')
>>> program(a, 'Name2', 'Detail2')
>>> a
[['Name1', ['Detail1', 'Counter'], ['Detail2', 'Counter']], ['Name2', ['Detail1', 'Counter'], ['Detail2', 'Counter']]]
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.