I have an assignment for Programming Fundamentals (we are using Python 3.4), and some questions are sometimes not really obvious to understand, maybe because my English is not so good.

Two of the introductory questions of our last assignment were:

  1. Write a function that “flattens” its input list.
  2. Write a function that accepts a list that contains nested lists and returns the “depth” of the list.

Sincerely I am not understanding exactly what they are asking. Can someone explain me?

  • Can you give us a link to the full context? The fact that two words are written "in quotes" means they don't have their normal meaning, so the question is effectively Off Topic anyway. But to give any kind of answer, it would help if we knew what things were covered in the course before this assignment was handed out. – FumbleFingers Reinstate Monica Nov 9 '14 at 18:37
  • @FumbleFingers We were talking about recursion and lists... – nbro Nov 9 '14 at 18:40
  • I've asked the mods to consider migrating this to Stack Overflow, where I've just found Making a flat list out of list of lists in Python (a duplicate, which implies they regularly deal with such concepts). – FumbleFingers Reinstate Monica Nov 9 '14 at 18:41
  • @FumbleFingers Ok, thanks, it might help, I will see... – nbro Nov 9 '14 at 18:42
  • 1
    OK. Looks like this was asked here because OP is currently prohibited from asking questions over at SO, so simply closing as OT. – Matt Nov 9 '14 at 23:45

Flattening a list in computer science means to eliminate any nested lists by incorporating them as elements, recursively, in the order they were specified.

For example, the Python nested list [1,2,3,[4,5,6],[7],[8,[9,10],11],12] would be flattened into the list of elements 1 through 12, inclusive. (Incidentally, this has a depth of 3 because the deepest elements 9 and 10 are inside a list inside a list inside a list.)

In Python and Lisp derivatives, flattening a nested list can be done textually by just removing all of the inner grouping symbols.

| improve this answer | |
  • 2
    Ah, thanks for the clarification. I'm guessing this doesn't really belong in English, it ought to be in 'programming' of some sort; as that's the knowledge you need to be able to parse the question correctly. – gone fishin' again. Nov 9 '14 at 19:53

Guessing a bit, as I'm not a programmer...

  1. "Flatten the list"
    Two possibilities, so I would really check with the test setter...

    • Empty the list, ready to start the task (most likely)
    • Take the nested lists & concatenate them into one list (less likely)
  2. "depth of the list"
    Again, I see two possibilities & I have no idea as to which may be more appropriate ...

    • Calculate the total number of list lines, including all nested lists
    • Calculate the number of levels used in the nesting.

A coder might know better.

| improve this answer | |
  • It's probably more your second options, but I am not sure. If a programmer could confirm, it would be great ;) – nbro Nov 9 '14 at 17:46
  • It means concatenate them in order into one list. – Jason S Nov 9 '14 at 19:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.