Q. What are the two ways to add something to a list? How are they different?


Answer =

The two ways to add something to a list that are :-

1 = list . append (<item>)
2 = list . insert (<pos>,<item>)

Difference :-
list . append (<item>) add the value as last of list .

example =

>>>name = [“ram”, “shyam”, “sita”]
>>>name . append (“ravi”)
>>>name
>>>[“ram”, “shyam”, “sita”, “ravi”]

While list.insert (<pos>,<item>) add the value by providing index number .

Example =

>>>name = [“ram”, “shyam”, “sita”]
>>>name . insert (1,“ravi”)
>>>name
>>> [“ram”, “ravi” “shyam”, “sita”]

6 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post