##### 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,330 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,330 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 Use one list as search key for another list with sublist of list jc4d 1 136 Nov-30-2021, 09:07 AM Last Post: Gribouillis sorting a list of lists by an element leapcfm 3 390 Sep-10-2021, 03:33 PM Last Post: leapcfm Why am I getting list elements < 0 ? Mark17 8 814 Aug-26-2021, 09:31 AM Last Post: naughtyCat Looping through nested elements and updating the original list Alex_James 3 499 Aug-19-2021, 12:05 PM Last Post: Alex_James Why changing data in a copied list changes the original list? plumberpy 3 640 Aug-14-2021, 02:26 AM Last Post: plumberpy Extracting Elements From A Website List knight2000 2 585 Jul-20-2021, 10:38 AM Last Post: knight2000 Make Groups with the List Elements quest 2 626 Jul-11-2021, 09:58 AM Last Post: perfringo Convert each element of a list to a string for processing tester_V 6 1,295 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 1,291 Jun-10-2021, 08:35 PM Last Post: perfringo I cannot delete and the elements from the list quest 4 1,091 May-11-2021, 12:01 PM Last Post: perfringo

Forum Jump:

### User Panel Messages

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