Number of occurrences of strings in array This is a Hackerrank problem (https://www.hackerrank.com/challenges/sparse-arrays/problem).
We're given an array of strings and an array of queries, and have to return an array of the number of occurrences of each query in the array of strings.
For example: s = ['ab', 'abc', 'aba', 'ab']. q = ['ab', atr', 'abc]. We need to return [2, 0, 1].
The constraints are 1 &lt;= n &lt;= 1000, 1 &lt;= q &lt;= 1000, where n and q are the number of strings and queries respectively, and 1 &lt;= |strings[i]|, |queries[i]| &lt;= 20.
Here is my code:
def matchingStrings(strings, queries):
    results = []
    dic = {q:0 for q in queries}
    for s in strings:
        if s in dic: dic[s] += 1
    for q in queries:
        if q in dic: results.append(dic[q]) 
    return results

Is there anything I can optimize? Speed, memory usage? Anything else?
if q in dic is pointless. You initialized dic so that it does have all queries.
dic = dict.fromkeys(queries, 0) should be a bit faster.
dic is not a particularly meaningful name, counter or ctr would be better.
Creating results at the start of the function gives the false impression that it's needed there already. I'd create it right before the loop that builds it. Then you'd have the complete counting part first, and the complete querying part afterwards. Or better yet, ...
... use a list comprehension, which is faster and nicer.
Python already has a Counter class that does a lot for you and is probably faster.

from collections import Counter

def matchingStrings(strings, queries):
    ctr = Counter(strings)
    return [ctr[q] for q in queries]

For fun: 1000*1000 isn't super big, and HackerRank's calling code really only needs an iterable, so... this also gets accepted:
def matchingStrings(strings, queries):
    return map(strings.count, queries)