Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

count function in python list

Hello comrades, I want to take a character from the input and convert it to a list, and then show the number of repetitions of each index to the user, but it gives an error.

my code:

list = list(input("plase enter keyword"))

for item in list:
    print(f"value({item})"+list.count(item))

my error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError 
Traceback (most recent call last)
c:\Users\emanull\Desktop\test py\main.py in <cell line: 3>()
      2 list = list(input("plase enter keyword"))
      4 for item in list:
----> 5     print(f"value({item})"+list.count(item))

TypeError: can only concatenate str (not "int") to str

>Solution :

list_ = list(input("plase enter keyword"))

for item in list_:
    print(f"value({item}) {list_.count(item)}")

OR

list_ = list(input("plase enter keyword"))

for item in list_:
    print(f"value({item})"+str(list_.count(item)))

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading