get index and value from list python code example

Example 1: python3 iterate through indexes

items=['baseball','basketball','football']
for index, item in enumerate(items):
    print(index, item)

Example 2: get index of list python

list.index(element)

Example 3: python list get index from value

["foo", "bar", "baz"].index("bar")

Example 4: python for loop index

presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe", "Adams", "Jackson"]
for num, name in enumerate(presidents, start=1):
    print("President {}: {}".format(num, name))

Example 5: get value from index python

# To get value from index in a list:
x = ["Foo", "Bar", "Roblox"]

print(x[0]) # Output: Foo

Example 6: python get index and value from list

test = [ 'test 1', 'test 2', 'test 3' ]
for index, value in enumerate( test ):
  print( index, value )