Horje
Make a Set of Lists in Python

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 Python

Below are various ways to make a set of lists in Python:

  • Using list() Function
  • Using Dictionary
  • Using List Comprehension
  • Using append() Function

Creating a Set of List Using list() Function

It 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

list1 = [1, 23, 65]
colors = ["red", "orange", "green"]
names = ["Diwakar", "Aniket", "Ritik"]
  
#Printing lists
print("List 1 : ",list1)
print("List 2 : ",colors)
print("List 3 : ",names)
  
# Organizing the lists into a set (using a list)
set_of_lists = [list1, colors, names]
  
# Printing the combined list
print("Combined list:", set_of_lists)

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 Dictionary

In 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

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
list3 = [10.5, 20.3, 30.7]
  
#Printing the list
print("First list :",list1)
print("Second list :",list2)
print("Third list :",list3)
  
# you can use a dictionary to name each list
named_set_of_lists = {'numbers': list1, 'letters': list2, 'floats': list3}
  
# Print the combined list
print("Combined list :",named_set_of_lists)

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 Comprehension

List 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

list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
  
#Print the lists
print("List 1 : ",list1)
print("List 2 : ",list2)
  
# Using set comprehension
set_of_lists = [list(lst) for lst in [list1, list2]]
  
#Printing the Combined List
print("The Combined List : ",set_of_lists)

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() Method

In 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

list1 = [1, 2, 3]
list2 = ['Charles', 'Jim', 'Bran','Arya']
  
#Printing the list
print("List 1 is : ",list1)
print("List 2 is : ",list2)
  
# Initializing an empty List
combined = []
  
# Using append() method
combined.append(list1)
combined.append(list2)
  
print("Combined list : ",combined)

Output

List 1 is :  [1, 2, 3]
List 2 is :  ['Charles', 'Jim', 'Bran', 'Arya']
Combined list :  [[1, 2, 3], ['Charles', 'Jim', 'Bran', 'Arya']]


Conclusion

We 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

Related
Get the Length of Text Without Using Inbuild Python Library Function Get the Length of Text Without Using Inbuild Python Library Function
Search for Value in the Python Dictionary with Multiple Values for a Key Search for Value in the Python Dictionary with Multiple Values for a Key
Run One Python Script From Another in Python Run One Python Script From Another in Python
Create A List of Lists Using For Loop Create A List of Lists Using For Loop
Install Alembic In Python Install Alembic In Python

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13