Skip variable number of iterations in Python for loop
Question: I have a list and a for loop such as these:

    
    
    mylist = ['foo','foo','foo','bar,'bar','hello']
    for item in mylist:
        cp = mylist.count(item)
        print("You "+item+" are present in "+str(cp)+" copy(ies)")
    

Output:

    
    
    You foo are present in 3 copy(ies)
    You foo are present in 3 copy(ies)
    You foo are present in 3 copy(ies)
    You bar are present in 2 copy(ies)
    You bar are present in 2 copy(ies)
    You dude are present in 1 copy(ies)
    

**Expected output:**

    
    
    You foo are present in 3 copy(ies)
    You bar are present in 2 copy(ies)
    You dude are present in 1 copy(ies)
    

The idea is thus to skip a variable number of iterations within the for loop,
using something like this script (**not working**):

    
    
    for item in mylist:
        cp = mylist.count(item)
        print("You "+item+" are present in "+str(cp)+" copy(ies)")
        continue(cp)
    

The script would thus "jump" `cp` elements in the for loop at every round and
start doing again what it is asked at the item `item + cp`.

I know that you can use `continue` to skip multiple iterations (such as in
[this post](http://stackoverflow.com/questions/22295901/skip-multiple-
iterations-in-loop-python)) but I cannot figure out how to use `continue` to
skip a **variable number of iterations**.

Thanks for your answer! :)

* * *

Edit: similar items are always next to each other.


Answer: You could use a `Counter`:

    
    
    from collections import Counter
    
    mylist = ['foo','foo','foo','bar','bar','hello']
    c = Counter(mylist)
    for item, cp in c.items():
        print("You "+item+" are present in "+str(cp)+" copy(ies)")
    

