List (list) in the append () and extend () method

append () method:

append () method is used at the end of the list to add a new element

Syntax: list.append (obj)

obj-- it added to the end of the dictionary objects

List = ['haha', 'yaya', 'lala']
Tuple = ('heihei', 'aa')
print(List)

 

 extend () method

Another plurality of values ​​used at the end of the sequence list additional disposable

Syntax: lIst.extend (seq)

seq - refers to a list of elements may be a list of tuples, sets, dictionary (if the dictionary, the dictionary will only keys (Key), successively adding the original end of the list)

List = ['haha', 'yaya', 'lala']
Tuple = ('heihei', 'aa')
Dict1 = {"Name": "lele", "Age": "18"}
List.append(1)
List.extend(Tuple)
List.extend(Dict1)
print(List)

 

 

append () method and extend () method distinction

append () can only append a new element at the end of the list

extend () can be added to a number of new elements at the end of the list

Guess you like

Origin www.cnblogs.com/keepkeep/p/11572504.html