You have a list like this:
L = [3, 2, 3, 1, 2, 0, 9, 9, 9, 2, 3]
Start with the first item, 3, at position 0 in the list.
Walk through the list, and write down the position if the element in
that position is the same as the element we are looking at, 3:
Position 0: does 3 equal 3? Yes it does, write down 0.
Position 1: does 3 equal 2? No.
Position 2: does 3 equal 3? Yes, write down 2.
...
Position 9: does 3 equal 2? No.
Position 10: does 3 equal 3? Yes, write down 10.
Now we move on to the next element in the list, 2, and do the same
thing:
Position 0: does 2 equal 3? No.
Position 1: does 2 equal 2? Yes, write down 1.
...
And so on, until you have done the entire list.
Is that clear?
Whenever you have to “do the same thing” multiple times, it makes sense
to write a function. So let’s start with a function that takes an
element as argument, and walks the list L returning the positions:
def collect_positions(element):
positions = an empty list
for position, item in enumerate(L):
does element equal item? if so,
append position to positions
(otherwise do nothing)
return positions
Obviously most of that is not legal Python code, you have to write that
yourself.
Now that we have a function that gives us the positions an element is
seen in, we can easily collect the positions of every element:
def collect_all_positions():
all_positions = an empty list
for element in L:
result = collect_positions(element)
append (position, result) to all_positions
return all_positions
If you successfully re-write that into working Python code, you will
discover that the “all positions” part doesn’t do exactly what you
have asked. I will leave the difference, and fixing it, as an exercise.
(Once you have the rest working, you can ask for a hint if you need it.)
By the way, if this is an exercise for college, your college probably
has hugely strict rules about plagiarism and copying. Ask your college
tutor how much of the help you get here needs to be declared.