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)

```
It looked interesting so I gave it a try. It will work with an arbitrary number of lists

```
a= [['house','table','house'],['desk','computer','table']]
from itertools import chain
d={}
for key in set(chain(*a)): #chain to get all the unique keys possible
    d.setdefault(key,[]) # set default value of every key as an empty list

for lis in a:           # Iterate over the list of lists

    for j in set(lis):  #convert the list to set as we do not want to count the items multiple times
        d[j].append(lis.count(j)) #append the count to the list associated with the key

    for key in set(d.keys())-set(lis): #keys which are present in power set but not in subset means item is not present
        d[key].append(0)    #append count as 0

print(d)

```

Voila you should get output like below

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

```