Grouping elements of the same indices means grouping elements of two or more data structures according to their indices. It is a tricky problem for beginners and can be asked in any coding interview. If you want to learn more about grouping items from the same index, this article is for you. In this article, I’ll walk you through a tutorial on how to group elements of the same indices using Python.
Group Elements of Same Indices using Python
To group elements of the same index, you will initially have two or more lists inside a list like [[a, b], [c, d]]. To group the elements of these lists, you need to create two new lists where you will store the elements of both the lists at index 0 [a, c] and index 1 [b, d]. That is the meaning of grouping the elements of the same indices.
Now below is how you can group the elements of the same indices using the Python programming language:
inputLists = [[10, 20, 30], [40, 50, 60], [70, 80, 90]] outputLists = [] index = 0 for i in range(len(inputLists[0])): outputLists.append([]) for j in range(len(inputLists)): outputLists[index].append(inputLists[j][ index]) index = index + 1 a, b, c = outputLists[0], outputLists[1], outputLists[2] print(a, b, c)
[10, 40, 70] [20, 50, 80] [30, 60, 90]
So this is how you can group the items of the same indices using Python. Find many more coding questions and projects solved and explained using Python here.
Summary
To group items of the same indices, you will initially have two or more lists inside a list like [[a, b], [c, d]], and you need to create two new lists where you will store the elements of both the lists at index 0 [a, c] and index 1 [b, d]. I hope you liked this article on grouping the elements of the same indices using Python. Feel free to ask valuable questions in the comments section below.
inputLists = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
a, b, c = inputLists
group = list(zip(a, b, c))
list_group = list(map(list, group))
Thank you Aman, these articles are very useful! My take on the problem is the following, it works for 2d ragged lists too:
def group_elements_by_same_indices(a_list):
result_list = []
# finding the maximum length of lists be it container or inner the longer:
index_range = range(max(len(a_list), max([len(lst) for lst in a_list])))
for indx in index_range:
temp_list = []
for lst in a_list:
if indx in range(len(lst)):
temp_list.append(lst[indx])
result_list.append(temp_list)
return result_list
lists_ragged = [[8, 9, 11, 30], [10, 20, 30], [40, 50, 60], [111, 222, 333, 444, 555]]
print(group_elements_by_same_indices(lists_ragged))