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)

```
Alternative more verbose but easy to follow.

First place the lists in a containing sueperlist:

```
superlist = [l1, l2]

```

Then initialise a dictionary (`word_count`) with the required keys (each word appearing in the lists):

```
word_count={}
for lst in superlist:
  for w in set(lst):
    word_count.setdefault(w, list())

```

Finally, iterate over the word\_count picking up the count of the encountered words:

```
for word in word_count.keys():
  for lst in superlist:
    word_count[word].append(lst.count(word))

```

Now `word_count` contains:

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

```

---

**All in a method for any number of list:**

```
def count_words(*superlist):
  word_count={}
  for lst in superlist:
    for w in set(lst):
      word_count.setdefault(w, list())
  for word in word_count.keys():
    for lst in superlist:
      word_count[word].append(lst.count(word))
  return word_count

print(count_words(l1, l2))
#=> {'table': [1, 1], 'house': [2, 0], 'desk': [0, 1], 'computer': [0, 1]}

```