Python Forum
List Indices Must Be Integers, Not Str.
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Indices Must Be Integers, Not Str.
#1
The following code SHOULD report the number of times that the word "fizz" appears inside of the list GroceryItems. However, I continually receive an error message saying that there is an issue with having x[y] because y is a string, and not a number.

How can I fix this script? And, this just being out of curiosity, why doesn't x[y] work? "y" is the incrementing variable in my "for" loop. It should be an integer.

def fizz_count(x):
    count = 0
    for y in x:
        if x[y] == "fizz":
            count = count + 1
    print count

GroceryItems = ["fizz","soda", "apple", "fizz", "fizz"]

fizz_count(GroceryItems)
Reply
#2
Run it this way -- see the value of print statements

def fizz_count(x):
   count = 0
   print(x)
   for y in x:
       print(y)
       if x[y] == "fizz":
           count = count + 1
   print count

GroceryItems = ["fizz","soda", "apple", "fizz", "fizz"]

fizz_count(GroceryItems)
Reply
#3
(Apr-18-2017, 12:23 AM)Pramerios Wrote: And, this just being out of curiosity, why doesn't x[y] work? "y" is the incrementing variable in my "for" loop. It should be an integer.
Nope.  For loops iterate over the actual contents of the iterable, not the indexes of it.  Normally that's what you want, but if you did want the index for some reason, you can use enumerate (although if you're doing that, you should take a good look at what you're doing, because it's probably not very ideomatic).
For example:
>>> GroceryItems = ["fizz","soda", "apple", "fizz", "fizz"]
>>> for item in GroceryItems:
...   print(item)
...
fizz
soda
apple
fizz
fizz
>>> for ndx, item in enumerate(GroceryItems):
...   print("{0}: {1}".format(ndx, item))
...
0: fizz
1: soda
2: apple
3: fizz
4: fizz
Reply
#4
There is no need for integer index in this task,and naming stuff x and y dos not help.
If need to manipulate index then enumerate() as shown bye @nilamo.
def fizz_count(groceries):
   count = 0
   for item in groceries:
       if item == "fizz":
           count += 1
   return count

groceries = ["fizz","soda", "apple", "fizz", "fizz"]
print(fizz_count(groceries)) #3
There also a count() method for list.
>>> groceries = ["fizz","soda", "apple", "fizz", "fizz"]
>>> groceries.count('fizz')
3
Reply
#5
(Apr-18-2017, 12:40 AM)Larz60+ Wrote: Run it this way -- see the value of print statements

......
       if x[y] == "fizz":
           count = count + 1
......
or just - couldn't resist Rolleyes
.....
        count += x[y] == 'fizz'
.....
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tuple indices must be integers or slices, not str cybertooth 16 11,079 Nov-02-2023, 01:20 PM
Last Post: brewer32
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,183 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,185 Mar-24-2023, 08:34 AM
Last Post: fullytotal
Question How to append integers from file to list? Milan 8 1,360 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 1,931 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,394 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 3,261 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  read a text file, find all integers, append to list oldtrafford 12 3,371 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,501 Apr-22-2022, 10:56 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020