0

Is there a way to skip the first iteration in this for-loop, so that I can put a for-loop inside a for-loop in order to compare the first element in the list with the rest of them.

from collections import Counter
vowelCounter = Counter()
vowelList = {'a','e','i','o','u'}
userString = input("Enter a string ")
displayed = False
for letter in userString:
    letter = letter.lower()
    if letter in vowelList:
        vowelCounter[letter] +=1
    for vowelCount1 in vowelCounter.items():
        char, count = vowelCount1
        for vowelCount2 in vowelCounter.items(STARTING AT 2)
            char2, count2 = vowelCount2
            if count > count2 : CONDITION

How would the syntax go for this? I only need to do a 5 deep For-loop. So the next would Start at 3, then start at 4, then 5, the the correct print statement depending on the condition. Thanks

3
  • It's already answered down there, just a note: use iteritems instead of items for iterating over a dictionary. It will be much more efficient for large dictionaries. Commented Nov 13, 2013 at 13:56
  • @leeladam - that's true, but only for Python 2.x. In Python 3.x there is no iteritems anymore, as items doesn't create a new list anymore. Commented Nov 13, 2013 at 14:04
  • Slicing a list with [1:] as suggested below creates a new array. It is faster and more economic to use a slice iterator with itertools.islice() - see code below Commented Oct 19, 2015 at 10:05

4 Answers 4

4

You could do:

for vowelCount2 in vowelCounter.items()[1:]:

This will give you all the elements of vowelCounter.items() except the first one.

The [1:] means you're slicing the list and it means: start at index 1 instead of at index 0. As such you're excluding the first element from the list.

If you want the index to depend on the previous loop you can do:

for i, vowelCount1 in enumerate(vowelCounter.items()):
    # ...
    for vowelCount2 in vowelCounter.items()[i:]:
        # ...

This means you're specifying i as the starting index and it depends on the index of vowelCount1. The function enumerate(mylist) gives you an index and an element of the list each time as you're iterating over mylist.

Sign up to request clarification or add additional context in comments.

6 Comments

Probably worth pointing out that "the first one" doesn't really have much meaning when dealing with a dict's .items() and that some form of "cartesian join" would be much better expressed using itertools.product
@JonClements: yes, that's true.
I've received the error: line 41, in <module> for vowelCount2 in vowelCounter.items()[1:]: TypeError: 'dict_items' object is not subscriptable
@JamesDonnelly ah, you're using Python 3. In that case you'd need to do list(vowelCounter.items()).
So how would that work? What would be the syntax doing the [1:]
|
2

It looks like what you want is to compare each count to every other count. While you can do what you suggested, a more succinct way might be to use itertools.combinations:

for v1,v2 in itertools.combinations(vowelCounter, 2):
    if vowelCounter[v1] > vowelCounter[v2]:
        # ...

This will iterate over all pairs of vowels for comparison. Doing it this way, you may also want to check if vowelCounter[v2] > vowelCounter[v1] as you won't see these two again (this goes for this method or the nested for loop method). Or, you can use the itertools.permutations function with the same arguments and just one check would suffice.

2 Comments

I get the error: line 62, in <module> for v1,v2 in itertools.combinations(vowelCounter, 2): NameError: name 'itertools' is not defined
@JamesDonnelly you'd need to add the line import itertools to import the itertools module.
2

To skip an iteration you can use the continue keyword eg:

list = [1,2,3,4,5,6,7,8,9,10]

for value in list:
  if value == list[0]:
    continue
  print(value)

Would give you:

2
3
4
5
6
7
8
9
10

I hope this answers your question.

Comments

0

Slicing a list with [1:] as suggested by a few others creates a new array. It is faster and more economic to use a slice iterator with itertools.islice()

from itertools import islice
for car in islice(cars, 1, None):
    # do something

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.