Python - Nested Records List from Lists


The problem at hand is that we have to create an algorithm for getting the nested records list from the given multiple lists with the help of Python. Sometimes we need to combine the given lists for a reason in real life applications. So this problem will be helpful to solve those problems.

Understanding the Logic for the Problem

In this problem we will be given two or more lists and we have to combine and form a nested records list by applying the logic. So we will use different approaches to do this task.

First we will use the zip method to zip all the lists given as input and the zip method will generate the single nested list. Second we will use the loop with the zip method and create a new list which will contain all the items of the given input list. Third we will solve this problem using the user defined function.

First Approach - Algorithm

In this approach we will use the zip method of Python which will combine the lists with respect to the indexing of the list items.

  • Step 1 − Initialize the lists. In our code we will use Name, DOB and Birth_Place lists.

  • Step 2 − Now using the new list as combined_lists this list will store the nested records list from the given lists. And using the zip method we will create this list by passing all the lists inside the zip method.

  • Step 3 − Print the combined_lists to show the Output.

Example

Name= ["John","Riya", "Amit"]

DOB = ["11-09-1998", "17-02-1993", "02-05-2003"]

Birth_Place = ["Delhi", "Mumbai", "Pune"]

combine_lists = zip(Name, DOB, Birth_Place)

print("The combined list for all the lists")

print(list(combine_lists))

Output

The combined list for all the lists
[('John', '11-09-1998', 'Delhi'), ('Riya', '17-02-1993', 'Mumbai'), ('Amit', '02-05-2003', 'Pune')]

Second Approach - Algorithm

It is yet another way of doing this task by using the for loop with zip method.

  • Step 1 − Define the list1 and list 2 which will be used to combine for creating the nested list.

  • Step 2 − Using the_key variable to add the key in the new list and its value will be id.

  • Step 3 − Now we will create a new list as nested_list and initialize it by creating the key for every item and then again create a nested list inside the outer list using zip method.

  • Step 4 − After getting the nested_list we will print the values of both input and Output lists.

Example

# Initialize the lists
list1 = ['Tea', 'Coffee']
list2 = [['Black', 'Green', 'White'], ['Espresso', 'Latte', 'Cappuccino']]

the_key = 'id'

nested_list = {key : [{the_key : index} for index in val]
   for key, val in zip(list1, list2)}

# Print the input lists
print("The first input list : " + str(list1))
print("The second input list : " + str(list2))

# Result print on the console
print("The constructed dictionary is : " + str(nested_list))

Output

The first input list : ['Tea', 'Coffee']
The second input list : [['Black', 'Green', 'White'], ['Espresso', 'Latte', 'Cappuccino']]
The constructed dictionary is : {'Tea': [{'id': 'Black'}, {'id': 'Green'}, {'id': 'White'}], 'Coffee': [{'id': 'Espresso'}, {'id': 'Latte'}, {'id': 'Cappuccino'}]}

Third Approach - Algorithm

In this approach we will define a function to generate the nested list using the multiple input lists.

  • Step 1 − First define the function called generate_nested_list and pass all the input lists in this as a parameter. Here we will use star or asterisk operator (*) to pass multiple variables using a single variable with an asterisk operator.

  • Step 2 − Now define the blank list as combined_list which will store the resultant nested list.

  • Step 3 − Now we will combine the lists using the for loop and append all the items in combined_list.

  • Step 4 − Print the combined_list by calling the function and pass all the lists in the function.

Example

# Define the function to generate nested list
def generate_nested_list(*lists):
   combined_list = []
   for lst in lists:
      combined_list.append(lst)
   return combined_list

#Initialize the lists to generate nested list
list1 = ['I', 'am', 'using']
list2 = ['Tutorials', 'point', 'to']
list3 = ['learn', 'Python', 'Programming']
nested_list = generate_nested_list(list1, list2, list3)
print("The created nested list is as follows\n", nested_list)

Output

The created nested list is as follows
 [['I', 'am', 'using'], ['Tutorials', 'point', 'to'], ['learn', 'Python', 'Programming']]

Complexity

The time complexity for creating new nested records lists from the given lists is O(n), here n is the total number of items present in all the lists. As we have performed simple operations on all the approaches which required linear time to complete the task.

Conclusion

As we have successively solved the given problem using various approaches in Python. So here we have learned how we can combine or create a nested list from the given multiple lists using simple methods. This task can be helpful when we need to merge and create a new data set from the given multiple data sets.

Updated on: 17-Oct-2023

46 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements