Python Forum
List problem "TypeError: must be str, not int"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List problem "TypeError: must be str, not int"
#1
I'm trying to print the elements of a list of strings to test Python's sorting methods, but it's complaining about data types:
#!/usr/bin/env python3
#ListSorting.py

def stringListSort():
    upperAndLowerSort = ["chicken", "pig", "Dink", "cow"]
    upperAndLowerSort.sort()
    for i in upperAndLowerSort:
        print(str(i))#print(upperAndLowerSort[i])
        i += 1
    keyArgSort = ["chicken", "pig", "Dink", "cow"]
    keyArgSort.sort(key=str.lower)
    for i in keyArgSort:
        print(i)
        i += 1

def main():
    stringListSort()

main()
Error:
========= RESTART: I:/Python/Python36-32/SamsPrograms/ListSorting.py ========= Dink Traceback (most recent call last): File "I:/Python/Python36-32/SamsPrograms/ListSorting.py", line 19, in <module> main() File "I:/Python/Python36-32/SamsPrograms/ListSorting.py", line 17, in main stringListSort() File "I:/Python/Python36-32/SamsPrograms/ListSorting.py", line 9, in stringListSort i += 1 TypeError: must be str, not int >>>
I understand that I'm not printing integer elements, but how do I get it to print the string elements via their list index?
And why is "Dink" the only thing printing out before generating an error?
Reply
#2
The i variable in for loop is not the index but rather a particular item from your list, over which it iterates. Try this:

my_list = ['a','b','c','d']
for item in my_list:
    print(item)
If you want to get index in each loop, you need to use enumerate() function, like this:

my_list = ['a','b','c','d']
for index, item in enumerate(my_list):
    print(index, item)
Quote:And why is "Dink" the only thing printing out before generating an error?
Now you probably understand why this happens. Line #8 gets executed properly before hitting the error in line #9. You print first item in the list, which is a string anyway.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,886 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,177 May-07-2022, 08:40 AM
Last Post: ibreeden
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,589 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  Problem with "Number List" problem on HackerRank Pnerd 5 2,123 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 3,015 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  TypeError: 'list' object is not callable Python_Mey 1 2,485 Aug-25-2020, 03:56 PM
Last Post: Yoriz
  Python Error- TypeError: ('Params must be in a list, tuple, or Row', 'HY000') DarkCoder2020 3 5,603 Jul-29-2020, 12:02 AM
Last Post: Larz60+
  [Solved]TypeError: list indices must be integers or slices, not str NectDz 3 3,934 Jun-02-2020, 08:21 AM
Last Post: DreamingInsanity
  TypeError: list indices must be integers or slices, not float hissonrr 2 3,323 Apr-19-2020, 12:02 AM
Last Post: hissonrr
  TypeError indexing a range of elements directly on the list JFerreira 2 2,212 Mar-30-2020, 04:22 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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