Python Program to Create a Class and Get All Possible Subsets from a List

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language

Object-Oriented Programming(OOPS):

Object-Oriented Programming (OOP) is a programming paradigm based on the concepts of classes and objects. It is used to organize a software program into simple, reusable code blueprints (typically referred to as classes), which are then used to build individual instances of objects. Object-oriented programming languages include JavaScript, C++, Java, and Python, among others.

A class is a generic blueprint that can be used to generate more specific, concrete things. Classes are frequently used to represent broad groups, such as Cars or Dogs, that share property. These classes indicate what properties, such as color, an instance of this type will have, but not the value of those attributes for a specific object.

An object comprises data, such as the raw or preprocessed materials at each step of an assembly line, as well as behavior, such as the action performed by each assembly line component.

Python, like other general-purpose programming languages, has always been an object-oriented language. It enables us to create a program with an Object-Oriented approach. Classes and objects are simple to build and utilize in Python.

Lists in Python:

In Python, a list is a data structure that works as a container for several values. A Python list is an ordered sequence of values that can be added to, deleted from, and replaced with new values. As a result, a list in Python can expand and contract in size. Each item in a list is referred to as an element, a list item, or simply an item.

Given a list, the task is to print all the subsets of the given list using classes.

Examples:

Example1:

Input:

given list = [4, 19, 2, 5, 3]

Output:

Subsets of the given list [4, 19, 2, 5, 3] :
[[], [19], [5], [5, 19], [4], [4, 19], [4, 5], [4, 5, 19], [3], [3, 19], [3, 5], [3, 5, 19], [3, 4], [3, 4, 19], [3, 4, 5], [3, 4, 5, 19],
 [2], [2, 19], [2, 5], [2, 5, 19], [2, 4], [2, 4, 19], [2, 4, 5], [2, 4, 5, 19], [2, 3], [2, 3, 19], [2, 3, 5], [2, 3, 5, 19], [2, 3, 4], 
[2, 3, 4, 19], [2, 3, 4, 5], [2, 3, 4, 5, 19]]

Example2:

Input:

given list = ['hello', 'this', 'is', 'btechgeeks']

Output:

Subsets of the given list ['hello', 'this', 'is', 'btechgeeks'] :
[[], ['this'], ['is'], ['is', 'this'], ['hello'], ['hello', 'this'], ['hello', 'is'], ['hello', 'is', 'this'], ['btechgeeks'], ['btechgeeks', 'this'],
 ['btechgeeks', 'is'], ['btechgeeks', 'is', 'this'], ['btechgeeks', 'hello'], ['btechgeeks', 'hello', 'this'], ['btechgeeks', 'hello',
 'is'], ['btechgeeks', 'hello', 'is', 'this']]

Program to Create a Class and Get All Possible Subsets from a List in Python

Below are the ways to print all the subsets of the given list using classes in Python.

i)Integer List

Approach:

  • Give the integer list as static input and store it in a variable.
  • Method sortlist() is used to pass an empty list and the user-supplied sorted list to method printSubsets().
  • Create an object to represent the class.
  • Call the sortlist method with the object created above.
  • To compute all possible subsets of the list, the method printSubsets() is called from sortlist() method.
  • The printSubsetsis used to return all possible sublists of the given list.
  • The Exit of the program.

Below is the implementation:

class sublist:
    def sortlist(self, given_list):
      # To compute all possible subsets of the list, the method
      # printSubsets() is called from sortlist() method.
        return self.printSubsets([], sorted(given_list))
    # The printSubsetsis used to return all possible sublists of the given list.

    def printSubsets(self, curr, given_list):
        if given_list:
            return self.printSubsets(curr, given_list[1:]) + self.printSubsets(curr + [given_list[0]], given_list[1:])
        return [curr]


# given list
given_list = [4, 19, 2, 5, 3]
# Create an object to represent the class.
listobj = sublist()
# Call the sortlist method with the object created above.
print("Subsets of the given list", given_list, ":")
print(sublist().sortlist(given_list))

Output:

Subsets of the given list [4, 19, 2, 5, 3] :
[[], [19], [5], [5, 19], [4], [4, 19], [4, 5], [4, 5, 19], [3], [3, 19], [3, 5], [3, 5, 19], [3, 4], [3, 4, 19], [3, 4, 5], [3, 4, 5, 19],
 [2], [2, 19], [2, 5], [2, 5, 19], [2, 4], [2, 4, 19], [2, 4, 5], [2, 4, 5, 19], [2, 3], [2, 3, 19], [2, 3, 5], [2, 3, 5, 19], [2, 3, 4], 
[2, 3, 4, 19], [2, 3, 4, 5], [2, 3, 4, 5, 19]]

ii)String list

Approach:

  • Give the string list as static input and store it in a variable.
  • Method sortlist() is used to pass an empty list and the user-supplied sorted list to method printSubsets().
  • Create an object to represent the class.
  • Call the sortlist method with the object created above.
  • To compute all possible subsets of the list, the method printSubsets() is called from sortlist() method.
  • The printSubsetsis used to return all possible sublists of the given list.
  • The Exit of the program.

Below is the implementation:

class sublist:
    def sortlist(self, given_list):
      # To compute all possible subsets of the list, the method
      # printSubsets() is called from sortlist() method.
        return self.printSubsets([], sorted(given_list))
    # The printSubsetsis used to return all possible sublists of the given list.

    def printSubsets(self, curr, given_list):
        if given_list:
            return self.printSubsets(curr, given_list[1:]) + self.printSubsets(curr + [given_list[0]], given_list[1:])
        return [curr]


# given list
given_list = ['hello', 'this', 'is', 'btechgeeks']
# Create an object to represent the class.
listobj = sublist()
# Call the sortlist method with the object created above.
print("Subsets of the given list", given_list, ":")
print(sublist().sortlist(given_list))

Output:

Subsets of the given list ['hello', 'this', 'is', 'btechgeeks'] :
[[], ['this'], ['is'], ['is', 'this'], ['hello'], ['hello', 'this'], ['hello', 'is'], ['hello', 'is', 'this'], ['btechgeeks'], ['btechgeeks', 'this'],
 ['btechgeeks', 'is'], ['btechgeeks', 'is', 'this'], ['btechgeeks', 'hello'], ['btechgeeks', 'hello', 'this'], ['btechgeeks', 'hello',
 'is'], ['btechgeeks', 'hello', 'is', 'this']]

Explanation:

  • A class called sublist is defined, as are the methods sortlist() and printSubsets.
  • The function sortlist of the class sub is then called, with the user’s list as a parameter.
  • Method sortlist, in turn, calls method printSubsets with an empty list and the user-supplied sorted list as inputs.
  • The printSubsets method is a recursive function.
  • This method computes all possible subsets by splits the given list into parts, starting with an empty list, and updating the given list’s value.
  • This process is repeated until the given list is empty.
  • The current value of the list is returned, which is a list containing subsets as separate lists.
  • The sublists are printed.

Related Programs: