Let's say I have the list:
lst1 = ['abc', 'abcde', 'abab', 'acd']
I want to create a dictionary to count the # of times 'ab' is in the list, so in this example, the dictionary would be {'ab': 4}. What do I do?
Let's say I have the list:
lst1 = ['abc', 'abcde', 'abab', 'acd']
I want to create a dictionary to count the # of times 'ab' is in the list, so in this example, the dictionary would be {'ab': 4}. What do I do?
You could use a generator expression within sum
>>> lst1 = ['abc', 'abcde', 'abab', 'acd']
>>> {'ab': sum(i.count('ab') for i in lst1)}
{'ab': 4}