Universal iteration over all elements of: nested list, numpy array, pandas dataframe
Question: I am trying to write a function that applies to each element of the argument
provided, whether the argument is a nested python list, a numpy array or a
pandas dataframe or series.

Example: (not working)

    
    
    import numpy as np
    import pandas as pd
    
    a = [[1,2,3,4,5], [6,7,8,9,10]]
    b = np.array(a)
    c = pd.DataFrame(a)
    
    def printElement(x):
        print x
    
    def iterateOverElements(arg):
        for element in arg:
            printElement(element)
    
    iterateOverElements(a)
    iterateOverElements(b)
    iterateOverElements(c)
    

Desired output: printing 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 in new lines for each
of the three calls. (Order is unimportant.)

True output:

    
    
    [1, 2, 3, 4, 5]
    [6, 7, 8, 9, 10]
    [1 2 3 4 5]
    [ 6  7  8  9 10]
    0
    1
    2
    3
    4
    

As expected, the nested list iterates over its sub-lists, the numpy array over
its rows and the pandas dataframe over its column labels.

**Is there a concise way to achieving my goal, or do I need to check the
instance of the argument and construct different iteration codes for different
types?** If I need to type-check, is there a way of importing e.g. pandas just
to type-check and find out that it was a nested list after all?

Again, I do not care about the order in which I iterate over the elements,
just that it is really the "basic" elements rather than some sub-collection.


Answer: I would suggest something like this:

    
    
    def printElements(arg):
        x = arg.iterrows() if isinstance(arg, pd.DataFrame) else arg
    
        try:
            for element in x:
                printElements(element)
        except:
            print x
    

