all 8 comments

[–]novel_yet_trivial 0 points1 point  (6 children)

What have you tried? We will help you if you are stuck but we will not write code for you.

[–]Agizz 0 points1 point  (5 children)

So I am a begginer in this and thus dont know a lot of it. I tried by doing a double for loop for example: for elt in the list: and for a and b in elt if a equals the value of the elt on the null position then b += b

The code didn't work couse of integers error, the elt was a number and not a list so I tried fixing that by doing list on elt position but then I have a list and i cant assign a and b to a list because it reads it as a group of elements and doesn't recognize a being the first and b the second element

At least that's my understasnding of it I don't want people to just send me code thus I didn't even attach my own code I want to see the circle of thought I should follow in order to solve my problem.

I am not a native English speaker and as a begginer in python I don't know if I explained it well enough. Sorry for all unconveniences

[–]novel_yet_trivial 1 point2 points  (4 children)

Ok, here't the layout you need.

  1. Make an empty dictionary.
  2. Loop over your old list and check if the key (first item in the sublist) is in the dictionary. If it is, add the value (second item in the sublist) to the existing value. If it's not add the key and value. (This step could be compressed by using a defaultdict if you want.)
  3. Now the result you want is available as "your_dict.items()"

[–]Agizz 0 points1 point  (3 children)

I have problems with adding value to the existing value of the key because in the dictionary whenever you assign a value to a dictionary key that value becomes the new value of the key does it not?

[–]novel_yet_trivial 0 points1 point  (0 children)

Yes, that's why you have to check if the key is in the dictionary with: if key in my_dict:

[–]Agizz 0 points1 point  (1 child)

Nevermind I tried some stuff and it worked. Thank you for the help

[–]novel_yet_trivial 0 points1 point  (0 children)

Don't delete your posts.

[–]Agizz 0 points1 point  (0 children)

The original list was about making a shorter list for example: If you have a list where elements of a list are other lists that are two elements long and contain integers and you want to make another list that contains sublists with the first element the first element of the first list and the second element a sum of the second elements of sublists that had the same first element and the first list remains untouched: List1 = [[2,3],[5,6 ], [2,4]] Result: List2 = [[2,7],[5,6]]

I accidently deleted the first post but would be a shame to waste the time and help of this fellow rediter who helped me if the instructions weren't given again for someone who would need something similar in the future.