Sum strings in a list

Dear Python Experts,

I started learning Python one month ago and I am not sure if I am doing the right thing to sum values in a list. I have the following list with car models and I would like to sum the total vehicle available. I tried several things and I am still reading the documentation on Python.org, but I am not sure if it is the simple way to find the total car models available in a list. Please, can one give a tip? Regards

carros = ["ferrari", "porsche", "bugatti", "ferrari", "porsche"]

total = [  ]
lista = [  ]

for key, value in enumerate(carros):
    num = int(key)
    total.append(num)

for nums in total:
    valores = total.count(nums)
    lista.append(valores)

soma = sum(lista)
print(soma)

Do you mean this?

carros = ["ferrari", "porsche", "bugatti", "ferrari", "porsche"]
print(len(carros))

len (short for “length”) gives the number of items in a list.

thank you Steven

What you are naming key, is in fact an index into carros. If you print(total), you’ll see the index values: [0, 1, 2, 3, 4] – 5 in total, one for each car.

Then in your second for: loop, valores will always be 1, because total.count(nums) will only ever hold one value, so you’ll get a list that is [1, 1, 1, 1, 1].

You can better what is going on with:

for key, value in enumerate(carros):
    print(f"{key}: {carros[key]}")

Or this, which will output the same:

for key, value in enumerate(carros):
    print(f"{key}: {value}")

Thank you Rob, but I think the len() solve my problem. I just want the total number of car available. Regards

You’re welcome and indeed that did provide what you needed; I was just illustrating some points that you may find of use, moving forward.