![]() |
Creating a set of lists typically involves defining multiple lists and organizing them into a structure, such as a dictionary or another list. In this article, we will see how we can make a set of lists in Python. Make a Set of Lists in PythonBelow are various ways to make a set of lists in Python:
Creating a Set of List Using list() FunctionIt takes a single item or an iterable item and converts it to a list. The list() constructor is a very easy and widely used method for converting an item into a list in Python. In this example, we are using list() function to combine multiple lists into a single list. Python3
Output
List 1 : [1, 23, 65] List 2 : ['red', 'orange', 'green'] List 3 : ['Diwakar', 'Aniket', 'Ritik'] Combined list: [[1, 23, 65], ['red', 'orange', 'green'], ['Diwakar', 'Aniket', 'Ritik']] Make A Set Of Lists Using DictionaryIn this method, our goal is to create a set of list using dictionary and name each list. Dictionary can also be used for creating a set of list in Python. Python3
Output
First list : [1, 2, 3] Second list : ['a', 'b', 'c'] Third list : [10.5, 20.3, 30.7] Combined list : {'numbers': [1, 2, 3], 'letters': ['a', 'b', 'c'], 'floats': [10.5, 20.3, 30.7]} Combine Multiple Lists Using List ComprehensionList Comprehension is used for solving our problem in less number lines of Code. But here we can also use list comprehension of Python for creating list of list in Python. Python3
Output
List 1 : [1, 2, 3] List 2 : ['a', 'b', 'c'] The Combined List : [[1, 2, 3], ['a', 'b', 'c']] Create a Set of Multiple Lists Using append() MethodIn this example, we are using append() method to create a set of lists in which we will append all the list into a single list creating a set of lists. Python3
Output
List 1 is : [1, 2, 3] List 2 is : ['Charles', 'Jim', 'Bran', 'Arya'] Combined list : [[1, 2, 3], ['Charles', 'Jim', 'Bran', 'Arya']] ConclusionWe can make the set of list by various ways, there are other methods to for making set of list in python. the above defined method are the common and known method for making set of list. You can choose any of the method as per your requirement and preference. |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |