Q:

Count the list items times of occurances

Let's assume I have the following list
names = ['tan','2','alp','3','tan','4','alp','3','tan','1']
the odd indexed elements are some values and even indexed elements are their number of occurrences (so alp happened 3+3 = 6 times for example)
I am trying to make code to do that.
enter code here
names = ['tan','2','alp','3','tan','4','alp','3','tan','1']

i = 1;
dd = names[0::2]
nn = names[1::2]
ct = [0 for cc in range(len(dd))];
le = len(dd);

for i in range(0, le):
    ct[i] = int(nn[i])
    for j in range(i+1, le):
        if (j < le) and (dd[i] == dd[j]):
            ct[i] += int(nn[j]);
            del(dd[j])
            # del(nn[j])
            le -= 1

the output i get for ct is [9, 7, 4, 3, 1]
however it should be [7, 6]
--- 7 for tan and 6 for alp
if i uncommented del(nn[j])
--- i  will be equal to le and code will stop
but i should delete the element and the number of occurrences it happened any time after first time (after adding it in ct for sure) so the counting process doesn't get repeated
any idea how to do that?

A:

That's relatively easy - build a lookup map from your odd elements and then sum the matching even values. You can use collections.defaultdict() to make your life easier:
import collections

names = ['tan', '2', 'alp', '3', 'tan', '4', 'alp', '3', 'tan', '1']

lookup_map = collections.defaultdict(int)
for element, value in zip(names[0::2], names[1::2]):
    lookup_map[element] += int(value)

print(dict(lookup_map))  # {'tan': 7, 'alp': 6}

If you really need only the values and need to keep the order, you can add an additional step:
result = [lookup_map.pop(element) for element in names[0::2] if element in lookup_map]

print(result)  # [7, 6]

If you're after the occurrences / total occurrences per element calculation, you can just apply the sum-total over the whole lookup map:
sum_total = sum(lookup_map.values())
for name in lookup_map:
    lookup_map[name] /= sum_total

print(dict(lookup_map))  # {'alp': 0.46153846153846156, 'tan': 0.5384615384615384}

