Iterating and Printing Data Structure Elements

  • Share this:

Code introduction


This function accepts a list or dictionary as an argument and iterates over its elements or key-value pairs to print them.


Technology Stack : List, dictionary, iteration, printing

Code Type : Function

Code Difficulty : Intermediate


                
                    
def aord_iterate(data):
    if isinstance(data, list):
        for item in data:
            print(item)
    elif isinstance(data, dict):
        for key, value in data.items():
            print(f"{key}: {value}")
    else:
        print(data)