Question: I would like to know how you can count the number of times the same element appears in different lists in Python. The number of lists is not determined

For example:

```
[house,table,house]
[desk,computer,table] 

```

would return a dictionary with 

```
{house:[2,0], table:[1,1], desk:[0,1]}

```

Answer: 
An easy way, if you don't want to import other tools, would be to use a [dictionary](https://www.w3schools.com/python/python_dictionaries.asp) to keep count.

```
list_1 = ['house','table','house']
list_2 = ['desk','computer','table'] 

# instantiate a dict that will contain the number of appearances in each list, as a list
appearances = {}

# loop through the first list
for item in list_1:
    # If the item hasn't appeared yet, it will set the item's name as the key and [1, 0] as the value
    # appearances[item] is how we're going to set the key name in appearances
    # appearances.get(item, 0)[0] makes it so that if item is not a key in appearances, it sets the initial value at index 0 to [0, 0]. 
    #   Otherwise, it sets it to +1 of it's current value
    # We set appearances[item] to a list so that we can keep track of appearances in both lists
    # For list_1's first item, appearances would then be equal to: appearances = {'house': [1, 0]}
    appearances[item] = [appearances.get(item, [0, 0])[0] + 1, 0]

# Do the same for the second list, but use index of 1, instead of 0
for item in list_2:
    # We use appearances.get() twice in case we haven't seen the item yet in either list
    appearances[item] = [appearances.get(item, [0, 0])[0], appearances.get(item, [0, 0])[1] + 1]

# Print the result
print(appearances)

```
You could create a function that accepts an arbitrary number of lists, which you will traverse and for each element that exists, you will count the number of occurences for each list:

```
list1 = ['house','table','house']
list2 = ['desk','computer','table']

def countElements(*lists):
    elements = set([elem for l in lists for elem in l])
    return dict((element,[l.count(element) for l in lists]) for element in elements)

print(countElements(list1,list2))

```

Result:

```
{'table': [1, 1], 'computer': [0, 1], 'house': [2, 0], 'desk': [0, 1]}

```

Alternatively you could store all those lists in another list and traverse them again with the same way:

```
list1 = ['house','table','house']
list2 = ['desk','computer','table']

def countElements(lists):
    elements = set([elem for l in lists for elem in l])
    return dict((element,[l.count(element) for l in lists]) for element in elements)

print(countElements([list1,list2]))

```

Notice the difference in function signature and in function call (the argument is a list of lists).