how to optimize python code? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 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