# Python – Convert Uneven Lists into Records

Sometimes, while working with Records, we can have a problem, that we have keys in one list and values in other. But sometimes, values can be multiple in order, like the scores or marks of particular subject. This type of problem can occur in school programming and development domains. Lets discuss certain ways in which this task can be performed.

Method #1 : Using dictionary comprehension + enumerate() + list slicing The combination of above functions can be used to solve this problem. In this, we iterate using dictionary comprehension and enumerate and form desired result by extracting records values by slicing.

## Python3

 `# Python3 code to demonstrate working of ` `# Convert Uneven Lists into Records` `# Using dictionary comprehension + enumerate() + list slicing`   `# initializing lists` `test_list1 ``=` `[``'Nikhil'``, ``'Akash'``, ``'Akshat'``]` `test_list2 ``=` `[``5``, ``6``, ``7``, ``8``, ``2``, ``3``, ``12``, ``2``, ``10``]`   `# printing original lists` `print``("The original ``list` `1` `is` `: " ``+` `str``(test_list1))` `print``("The original ``list` `2` `is` `: " ``+` `str``(test_list2))`   `# Convert Uneven Lists into Records` `# Using dictionary comprehension + enumerate() + list slicing` `temp ``=` `len``(test_list2) ``/``/` `len``(test_list1)` `res ``=` `{key: test_list2[idx :: temp] ``for` `idx, key ``in` `enumerate``(test_list1)}`   `# printing result ` `print``("The paired data dictionary : " ``+` `str``(res)) `

Output :

The original list 1 is : [‘Nikhil’, ‘Akash’, ‘Akshat’] The original list 2 is : [5, 6, 7, 8, 2, 3, 12, 2, 10] The paired data dictionary : {‘Nikhil’: [5, 8, 12], ‘Akshat’: [7, 3, 10], ‘Akash’: [6, 2, 2]}

Time Complexity: O(n*n) where n is the total number of values in the list “test_list”.
Auxiliary Space: O(n) where n is the total number of values in the list “test_list”.

Method #2 : Using list comprehension + zip() This task can also be solved using above functions. In this, we use list comprehension to construct the cumulated score list. In last step we tie both lists together using zip().

## Python3

 `# Python3 code to demonstrate working of ` `# Convert Uneven Lists into Records` `# Using list comprehension + zip()`   `# initializing lists` `test_list1 ``=` `[``'Nikhil'``, ``'Akash'``, ``'Akshat'``]` `test_list2 ``=` `[``5``, ``6``, ``7``, ``8``, ``2``, ``3``, ``12``, ``2``, ``10``]`   `# printing original lists` `print``("The original ``list` `1` `is` `: " ``+` `str``(test_list1))` `print``("The original ``list` `2` `is` `: " ``+` `str``(test_list2))`   `# Convert Uneven Lists into Records` `# Using list comprehension + zip()` `temp ``=` `len``(test_list2) ``/``/` `len``(test_list1)` `res ``=` `[test_list2[idx :: temp] ``for` `idx ``in` `range``(``0``, ``len``(test_list1))]` `res ``=` `dict``(``zip``(test_list1, res))`   `# printing result ` `print``("The paired data dictionary : " ``+` `str``(res)) `

Output :

The original list 1 is : [‘Nikhil’, ‘Akash’, ‘Akshat’] The original list 2 is : [5, 6, 7, 8, 2, 3, 12, 2, 10] The paired data dictionary : {‘Nikhil’: [5, 8, 12], ‘Akshat’: [7, 3, 10], ‘Akash’: [6, 2, 2]}

Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using the using list comprehension + zip() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next