Q:

How can I number each element in my list for a given range n?

I am trying to number elements in my nested list for a given range for the given function:
numbered_elements(list, n)

Use the following list as example:
fruits = [ ['GRAPE', 'APPLE'], ['ORANGE'], ['BANANA', 'PEAR'] ]

And,
n = 3

I want a function that will take as input the list fruits and n=3 and outputs the following:
[ ['GRAPE0', 'GRAPE1', 'GRAPE2', 'APPLE0', 'APPLE1', 'APPLE2'], ['ORANGE0', 'ORANGE1', 'ORANGE2'], ['BANANA0', 'BANANA1', 'BANANA2', 'PEAR0', 'PEAR1', 'PEAR2'] ] 

Here is the code that I have thus far:
empty = []
for l in fruits:
    for element in l:
        for a in range(n):
            empty.append(element + str(a))
return empty

This function is returning:
  ['GRAPE0', 'GRAPE1', 'GRAPE2', 'APPLE0', 'APPLE1', 'APPLE2', 'ORANGE0', 'ORANGE1', 'ORANGE2', 'BANANA0', 'BANANA1', 'BANANA2', 'PEAR0', 'PEAR1', 'PEAR2']

So what I'm trying to figure out is how to still keep it as a nested list.

A:

You have a nested list: you need to handle your appending in a loop as well, clearing the accretion list for each new outer-level list.
fruits = [ ['GRAPE', 'APPLE'], ['ORANGE'], ['BANANA', 'PEAR'] ]
n = 3

roster = []    # Clear the final output list only once
for l in fruits:
    empty = []          # Clear the suffix list for each new sub-list of fruit
    for element in l:
        for a in range(n):
            empty.append(element + str(a))
    roster.append(empty)

print(roster)

Output (line feeds inserted for readability):
[['GRAPE0', 'GRAPE1', 'GRAPE2', 'APPLE0', 'APPLE1', 'APPLE2'],
 ['ORANGE0', 'ORANGE1', 'ORANGE2'],
 ['BANANA0', 'BANANA1', 'BANANA2', 'PEAR0', 'PEAR1', 'PEAR2']]

