how to optimize python code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

how to optimize python code?

I have a list of lists Like: l = [ ["Mike", 25, 1500], ["Jane", 23, 2000], ["Mike", 25, 750], ] and if first two elements of inner lists are same I want to merge them and sum the third element of them. Like: res = [ ["Mike", 25, 2250], ["Jane", 23, 2000], ] This is my code: https://code.sololearn.com/c7AQiANvinWd/?ref=app

17th Jan 2021, 6:09 PM
Shadoff
Shadoff - avatar
2 Answers
+ 1
Hisham YUM 🇲🇦 set is useful to remove duplicate, however in this case it is not useful. Shadoff why are you using a list for this kind of data ? a dictionary will make the parsing easier. you can then create a key for the name and do comparaison
17th Jan 2021, 10:31 PM
Gaëtan Gr
Gaëtan Gr - avatar
0
Try using Sets instead! Think about it 😉
17th Jan 2021, 10:21 PM
Hisham YUM 🇲🇦
Hisham YUM 🇲🇦 - avatar