##### Adding List Element if Second part of the List Elements are the Same
 Adding List Element if Second part of the List Elements are the Same quest_ Silly Frenchman Posts: 39 Threads: 11 Joined: Nov 2020 Reputation: Nov-25-2020, 07:35 AM Hello, I have this list newlist = [[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]],(1.57, 1.57, 0)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 0)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 1.57)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 0, 0)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 0)]]]] Here I want to compare second part of the list (this part: (1.57, 1.57, 0), (1.57, 1.57, 0), (1.57, 1.57, 1.57).. ) and if the second parts of the list are same? I want to add them together. For instance the list which I wrote above we have 2 times (0, 1.57, 0) so the new list should be like that: newlist2 = [[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]],(1.57, 1.57, 0)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1],[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]]], (0, 1.57, 0), (0, 1.57, 0)# here instead of adding(0, 1.57, 0) again we can just put 2 for remembering we had 2 times (0, 1.57, 0)] [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 1.57)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 0, 0)]]] How can I do that in an efficient way Thanks for helps.. Reply bowlofred Da Bishop Posts: 1,190 Threads: 3 Joined: Mar 2020 Reputation: Nov-25-2020, 07:56 AM (This post was last modified: Nov-25-2020, 07:57 AM by bowlofred.) Trying to do this entirely within a list seems possible, but annoying to me. You're treating the bit on the end like a dictionary key. So I'd recommend creating a real dictionary instead. Then if you need the lists later, reassemble them from the dictionary. Actually storing the data in a dictionary seems much better than forcing everything into a four-deep nested list structure. ```from collections import defaultdict import pprint newlist = [[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]],(1.57, 1.57, 0)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 0)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 1.57)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 0, 0)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 0)]] info = defaultdict(list) #dict from the list for row in newlist: sublist, key = row info[key].append(sublist) # reassmble the list newlist = [] for key, value in info.items(): sublist = value sublist.append(key) newlist.append(value) pprint.pprint(newlist)`````````Output:[[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (1.57, 1.57, 0)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], [[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 0)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 1.57)], [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 0, 0)]]`````` quest_ likes this post Reply quest_ Silly Frenchman Posts: 39 Threads: 11 Joined: Nov 2020 Reputation: Nov-25-2020, 11:03 AM (This post was last modified: Nov-25-2020, 11:03 AM by quest_.) It worked thanks and is it possible to save in the list how many times we found second part. For intance: ``````Output:[[[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (1.57, 1.57, 0)], 1, [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], [[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 0)], 2, [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 1.57, 1.57)],1 [[[0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 0, 1], [1, 1, 1]], (0, 0, 0)],1]`````` Reply bowlofred Da Bishop Posts: 1,190 Threads: 3 Joined: Mar 2020 Reputation: Nov-25-2020, 04:33 PM The way it is now, the "key" is at the end of all the added lists. So you could just count the size of the list and subtract 1. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Extracting Elements From A Website List knight2000 2 215 Jul-20-2021, 10:38 AM Last Post: knight2000 Make Groups with the List Elements quest 2 222 Jul-11-2021, 09:58 AM Last Post: perfringo Convert each element of a list to a string for processing tester_V 6 545 Jun-16-2021, 02:11 AM Last Post: tester_V Compile list of dictianories out of another list of dictianories by certain keys CatorCanulis 10 638 Jun-10-2021, 08:35 PM Last Post: perfringo I cannot delete and the elements from the list quest 4 617 May-11-2021, 12:01 PM Last Post: perfringo List of lists - merge sublists with common elements medatib531 1 517 May-09-2021, 07:49 AM Last Post: Gribouillis Find Common Elements in 2 list quest 4 607 Apr-14-2021, 03:57 PM Last Post: quest Order a list with successive permutations based on another list yvrob 3 608 Mar-19-2021, 08:20 AM Last Post: supuflounder Saving list in a list quest_ 3 737 Mar-10-2021, 09:58 AM Last Post: quest_ Convert Bytearray into List using list() Shlok 2 507 Feb-18-2021, 10:44 AM Last Post: deanhystad

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020