{"id":3670,"date":"2023-10-23T19:28:58","date_gmt":"2023-10-23T13:58:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=3670"},"modified":"2023-11-10T11:55:10","modified_gmt":"2023-11-10T06:25:10","slug":"python-check-if-a-list-contains-all-the-elements-of-another-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-check-if-a-list-contains-all-the-elements-of-another-list\/","title":{"rendered":"Python : Check if a List Contains all the Elements of Another List"},"content":{"rendered":"
In Python, the list datatype\u00a0is that the\u00a0<\/span>most versatile, and it\u00a0are often\u00a0<\/span>written as\u00a0an inventory\u00a0<\/span>of comma-separated values (items) enclosed in square brackets.<\/p>\n Given two lists, the task is to check whether the second list has all the elements of first list.<\/p>\n Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Input:<\/strong><\/p>\n There are several ways to check if a list is contained in another list some of them are:<\/p>\n The function any() returns True if any of the items in an iterable are true; otherwise, False. The any() function returns False if the iterable object is empty.<\/p>\n Convert list2 to Iterable and check if any element in Iterable, i.e. list2, exists in list1.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n In a single line, the all() function is used to check all the elements of a container. Checks if all elements from one list are present in another list.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The most commonly used and recommended method for searching for a sublist. This function is specifically designed to determine whether one list is a subset of another.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n In Python, the list datatype\u00a0is that the\u00a0most versatile, and it\u00a0are often\u00a0written as\u00a0an inventory\u00a0of comma-separated values (items) enclosed in square brackets. Given two lists, the task is to check whether the second list has all the elements of first list. Examples: Input: givenlist1 = [‘Hello’ , ‘this’, ‘is’, ‘Btech’, ‘Geeks’] givenlist2 = [‘Btech’,’Geeks’] Output: givenlist2\u00a0has\u00a0all of …<\/p>\ngivenlist1 = <\/span>[<\/span>'Hello'<\/span> , <\/span>'this'<\/span>, <\/span>'is'<\/span>, <\/span>'Btech'<\/span>, <\/span>'Geeks'<\/span>]\r\n<\/span>givenlist2 = [<\/span>'Btech','Geeks'<\/span>]<\/span><\/pre>\n<\/div>\n
givenlist2\u00a0has\u00a0all of the \u00a0givenlist1\u00a0elements<\/pre>\n<\/div>\n
givenlist1 = <\/span>[<\/span>'Hello'<\/span> , <\/span>'this'<\/span>, <\/span>'is'<\/span>, <\/span>'Btech'<\/span>, <\/span>'Geeks'<\/span>]\r\n<\/span>givenlist2 = [<\/span>'Btech','Geeks','pink'<\/span>]<\/span><\/pre>\n<\/div>\n
givenlist2 doesn't have all of the \u00a0givenlist1 elements<\/pre>\n<\/div>\n
Determine whether \u00a0a list is included inside another list<\/h2>\n
\n
Method #1:Using any() function<\/h3>\n
# given two lists\r\ngivenlist1 = ['Hello', 'this', 'is', 'Btech', 'Geeks']\r\ngivenlist2 = ['Btech', 'Geeks']\r\n# using any()\r\nif(any(element in givenlist1 for element in givenlist2)):\r\n print(\"givenlist2 has all of the\u00a0\u00a0givenlist1 elements\")\r\nelse:\r\n print(\"givenlist2\u00a0doesn't\u00a0have\u00a0all\u00a0of\u00a0the\u00a0\u00a0givenlist1\u00a0elements\")\r\n<\/pre>\n
givenlist2\u00a0has\u00a0all of the \u00a0givenlist1\u00a0elements<\/pre>\n
Method #2:Using all() function<\/h3>\n
# given two lists\r\ngivenlist1 = ['Hello', 'this', 'is', 'Btech', 'Geeks']\r\ngivenlist2 = ['Btech', 'Geeks']\r\n# using all()\r\nif(all(element in givenlist1 for element in givenlist2)):\r\n print(\"givenlist2 has all of the\u00a0\u00a0givenlist1 elements\")\r\nelse:\r\n print(\"givenlist2\u00a0doesn't\u00a0have\u00a0all\u00a0of\u00a0the\u00a0\u00a0givenlist1\u00a0elements\")\r\n<\/pre>\n
givenlist2\u00a0has\u00a0all of the \u00a0givenlist1\u00a0elements<\/pre>\n
Method #3:Using set.issubset()<\/h3>\n
# given two lists\r\ngivenlist1 = ['Hello', 'this', 'is', 'Btech', 'Geeks']\r\ngivenlist2 = ['Btech', 'Geeks']\r\n# using set.issubset() function\r\nif(set(givenlist2).issubset(set(givenlist1))):\r\n print(\"givenlist2 has all of the\u00a0\u00a0givenlist1 elements\")\r\nelse:\r\n print(\"givenlist2\u00a0doesn't\u00a0have\u00a0all\u00a0of\u00a0the\u00a0\u00a0givenlist1\u00a0elements\")\r\n<\/pre>\n
givenlist2\u00a0has\u00a0all of the \u00a0givenlist1\u00a0elements<\/pre>\n
\n