w3resource

Python: Common elements in a given list of lists

Python List: Exercise - 177 with Solution

Write a Python program to find common elements in a given list of lists.

Visual Presentation:

Python List: Common elements in a given list of lists.

Sample Solution:

Python Code:

# Define a function called 'common_list_of_lists' that finds the common elements among multiple lists 'lst'.
def common_list_of_lists(lst):
    # Use set intersection to find the common elements among the lists in 'lst'.
    temp = set(lst[0]).intersection(*lst)
    # Convert the result into a list and return it.
    return list(temp)

# Create a list of lists 'nums' containing sublists of integers.
nums = [[7, 2, 3, 4, 7], [9, 2, 3, 2, 5], [8, 2, 3, 4, 4]]

# Print a message indicating the original list of lists.
print("Original list:")
print(nums)

# Call the 'common_list_of_lists' function to find the common elements among the sublists in 'nums' and print the result.
print("\nCommon elements of the said list of lists:")
print(common_list_of_lists(nums))

# Create a list of lists 'chars' containing sublists of characters.
chars = [['a', 'b', 'c'], ['b', 'c', 'd'], ['c', 'd', 'e']]

# Print a message indicating the original list of lists.
print("\nOriginal list:")
print(chars)

# Call the 'common_list_of_lists' function to find the common elements among the sublists in 'chars' and print the result.
print("\nCommon elements of the said list of lists:")
print(common_list_of_lists(chars)) 

Sample Output:

Original list:
[[7, 2, 3, 4, 7], [9, 2, 3, 2, 5], [8, 2, 3, 4, 4]]

Common elements of the said list of lists:
[2, 3]

Original list:
[['a', 'b', 'c'], ['b', 'c', 'd'], ['c', 'd', 'e']]

Common elements of the said list of lists:
['c']

Flowchart:

Flowchart: Common elements in a given list of lists.

Python Code Editor:

Previous: Write a Python program to create a new list dividing two given lists of numbers.
Next: Write a Python program to insert a specified element in a given list after every nth element.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/list/python-data-type-list-exercise-177.php