get a random item from a python list

import random # Define a new list using [] square brackets. fruits = ['banana', 'apple', 'orange', 'lemon', 'lime', 'strawberry', 'kiwi', 'peach', 'grape', 'pineapple', 'mango'] # Randomly select an item from a list print('=== Get a random item ===') print(random.choice(fruits)) # output run 1 === Get a random item === lime # output run 2 === Get a random item === pineapple # output run 3 === Get a random item === kiwi Read More →

iterate over a list with access to index and value

# Define a new list using [] square brackets. fruits = ['banana', 'apple', 'orange', 'lemon', 'lime', 'strawberry', 'kiwi', 'peach', 'grape', 'pineapple', 'mango'] # iterate over a list with access to index and value print('=== Iterate and print index and value of items in a list ===') for key, val in enumerate(fruits): print(key, ":", val) # output === Iterate and print index and value of items in a list === 0 : banana 1 : apple 2 : orange 3 : lemon 4 : lime 5 : strawberry 6 : kiwi 7 : peach 8 : grape 9 : pineapple 10 : mango Read More →