Python | Reshape a list according to given multi list
Given two lists, a single dimensional and a multidimensional list, write Python program to reshape the single dimensional list according to the length of multidimensional list.
Examples:
Input : list1 = [[1], [2, 3], [4, 5, 6]] list2 = ['a', 'b', 'c', 'd', 'e', 'f'] Output : [['a'], ['b', 'c'], ['d', 'e', 'f']] Input : list1 = [[8, 2, 5], [1], [12, 4, 0, 24]] list2 = ['m', 'n', 'o', 'p', 'q', 'r', 's', 't'] Output : [['m', 'n', 'o'], ['p'], ['q', 'r', 's', 't']]
Method #1 : Using extended slices
A simple and naive method is to use a for loop and Python extended slices to append each sublist of list2 to a variable ‘res’.
# Python3 program to reshape a list # according to multidimensional list def reshape(lst1, lst2): last = 0 res = [] for ele in list1: res.append(list2[last : last + len (ele)]) last + = len (ele) return res # Driver code list1 = [[ 1 ], [ 2 , 3 ], [ 4 , 5 , 6 ]] list2 = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ] print (reshape(list1, list2)) |
[['a'], ['b', 'c'], ['d', 'e', 'f']]
Method #2 : islice from itertools module
Another method is to use islice function from itertools module. islice selectively prints the values mentioned in its iterable container. Thus, we yield slices of list2 according to list1 and append it to variable ‘res’.
# Python3 program to reshape a list # according to multidimensional list from itertools import islice def yieldSublist(lst1, lst2): iter2 = iter (lst2) for sublist1 in lst1: sublist2 = list (islice(iter2, len (sublist1))) yield sublist2 def reshape(lst1, lst2): res = list () for sub2 in yieldSublist(list1, list2): res.append(sub2) return res # Driver code list1 = [[ 1 ], [ 2 , 3 ], [ 4 , 5 , 6 ]] list2 = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ] print (reshape(list1, list2)) |
[['a'], ['b', 'c'], ['d', 'e', 'f']]
Method #3 : Python iterator with list comprehension
The iter() method returns an iterator for list2 and save it in variable ‘iterator’. Now using list comprehension reshape list2 according to list1.
# Python3 program to reshape a list # according to multidimensional list def reshape(lst1, lst2): iterator = iter (lst2) return [[ next (iterator) for _ in sublist] for sublist in lst1] # Driver code list1 = [[ 1 ], [ 2 , 3 ], [ 4 , 5 , 6 ]] list2 = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ] print (reshape(list1, list2)) |
[['a'], ['b', 'c'], ['d', 'e', 'f']]
Recommended Posts:
- Python | Add list elements with a multi-list based on index
- Python | Group elements at same indices in a multi-list
- Python | Convert list of tuples to list of list
- Python | Convert list of string to list of list
- Python | Convert mixed data types tuple list to string list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Merge List with common elements in a List of Lists
- Python | Merge list of tuple into list by joining the strings
- Python | Find maximum length sub-list in a nested list
- Python | Sorting list of lists with similar list elements
- Python | Convert list of string into sorted list of integer
- Python | Replace elements in second list with index of same element in first list
- Python | Pair and combine nested list to tuple list
- Python | Filter list of strings based on the substring list
- Python | Convert string List to Nested Character List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.