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”]
Here, in insert we have to mention index number in eg...?🙂🙂
ReplyDeleteOk, We Have corrected it : )
DeleteCorrection in second line
ReplyDelete>>> name.insert (1,"Ravi")
Ok, We Have corrected it : )
DeleteIn 2nd line it is or ?
ReplyDelete?????
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )