Make a Set of Lists in Python
Last Updated :
09 Oct, 2024
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.
Below are various ways to make a set of lists in Python
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.
Python
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)
OutputList 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.
Python
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)
OutputFirst 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.
Python
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)
OutputList 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.
Python
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)
OutputList 1 is : [1, 2, 3]
List 2 is : ['Charles', 'Jim', 'Bran', 'Arya']
Combined list : [[1, 2, 3], ['Charles', 'Jim', 'Bran', 'Arya']]
Similar Reads
Creating a List of Sets in Python Creating a list of sets in Python involves storing multiple sets within list structure. Each set can contain unique elements and the list can hold as many sets as needed. In this article, we will see how to create a list of set using different methods in Python.Using List ComprehensionList comprehen
2 min read
Create a List of Strings in Python Creating a list of strings in Python is easy and helps in managing collections of text. For example, if we have names of people in a group, we can store them in a list. We can create a list of strings by using Square Brackets [] . We just need to type the strings inside the brackets and separate the
3 min read
Python - Create List of Size n Creating a list of size n in Python can be done in several ways. The easiest and most efficient way to create a list of size n is by multiplying a list. This method works well if we want to fill the list with a default value, like 0 or None.Python# Size of the list n = 5 # Creating a list of size n
2 min read
Print a List of Tuples in Python The task of printing a list of tuples in Python involves displaying the elements of a list where each item is a tuple. A tuple is an ordered collection of elements enclosed in parentheses ( ), while a list is an ordered collection enclosed in square brackets [ ].Using print()print() function is the
2 min read
How to Split Lists in Python? Lists in Python are a powerful and versatile data structure. In many situations, we might need to split a list into smaller sublists for various operations such as processing data in chunks, grouping items or creating multiple lists from a single source. Let's explore different methods to split list
3 min read
Convert set into a list in Python In Python, sets are unordered collections of unique elements. While they're great for membership tests and eliminating duplicates, sometimes you may need to convert a set into a list to perform operations like indexing, slicing, or sorting. For example, if input set is {1, 2, 3, 4} then Output shoul
3 min read