Which code is more preferred to add item to list..? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Which code is more preferred to add item to list..?

I think using" += "to add an item to a list is easier than using " .append ".....when it does the same job why use .append

25th May 2018, 12:04 PM
Abhinav Singh
Abhinav Singh - avatar
10 Answers
+ 3
There are many ways or approaches to do the same thing, many libraries and functionalities that get to the same result, but the app's performance is impacted by the approach used. Consider ArrayList vs LinkedList in java, both are flexible and very similar lists, but ArrayList is best working from the first index to the last in line, and LinkedList is best looking at a specific index due to the algorithms on them. The append just move to the last position and add the value opening the next index, while the "+=" should move through the entire list compering values, and then adding the difference, in the end you should get the same result just not the same way.
25th May 2018, 12:21 PM
Roberto Guisarre
Roberto Guisarre - avatar
+ 3
@Abhinav list+=list works like list.extend(list) list.append is for individual lists. Anyways, if lists have the __iadd__ magic method defined, obviously they can do that.
25th May 2018, 12:31 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
hii
25th May 2018, 12:05 PM
Kava Disha
+ 2
thank you.
25th May 2018, 12:22 PM
Abhinav Singh
Abhinav Singh - avatar
+ 1
oof You mixed up ArrayList and LinkedList.
25th May 2018, 12:23 PM
BlazingMagpie
BlazingMagpie - avatar
0
hey
25th May 2018, 12:06 PM
Abhinav Singh
Abhinav Singh - avatar
0
You won't like Java.
25th May 2018, 12:06 PM
BlazingMagpie
BlazingMagpie - avatar
0
y so?
25th May 2018, 12:07 PM
Abhinav Singh
Abhinav Singh - avatar
0
Unless you're madman who uses naked arrays, you have to use methods to add objects to containers.
25th May 2018, 12:09 PM
BlazingMagpie
BlazingMagpie - avatar
0
okay
25th May 2018, 12:12 PM
Abhinav Singh
Abhinav Singh - avatar