This is as much as I know how to do, not sure if I’m doing it correctly.
L = [4, 10, 4, 2, 9, 5, 4 ]
n = len(L)
element = ()
if element in L:
print(element)
print("number occurs in list at the following position, if element not in list")
print("this number does not occur in the list")
How do I go about getting elements that appear more than once, to print as
4 occurs in L at the following positions: [0, 2, 6]
You could use a list comprehension:
enumerate(L)gives you an iterator overLthat yields a tuple(index, value)for each element ofL. So what I’m doing here is take each index (i) if the value (x) equals4, and construct a list from them. No need to look at the length of the list.