Search code examples
listdictionarylist-comprehension

Calling a function with a list argument in Python


test = [
    {'input':{'nums': [19, 25, 29, 3, 5, 6, 7, 9, 11, 14]}, 'output': 3},
    {'input':{'nums': [6, 8, 9, 10, 11, 1, 3, 5]}, 'output': 5}
]

def count_rotations_binary(nums):
    pass

How can I call this function with the above list?


Solution

  • You can simply provide the name of the list as the actual argument when you are invoking the function. You don't need to supply any expected type in the function declaration.

    test = [
        {'input': {'nums': [19, 25, 29, 3, 5, 6, 7, 9, 11, 14]}, 'output': 3},
        {'input': {'nums': [6, 8, 9, 10, 11, 1, 3, 5]}, 'output': 5}
    ]
    
    # This prints whatever you give to it
    def count_rotations_binary(nums):
        print(nums)
    
    
    # Pass the entire list
    count_rotations_binary(test)
    
    # Pass only the 'nums' value
    for item in test:
        count_rotations_binary(item['input']['nums'])