Tag Archives: AttributeError: ‘tuple’ object has no attribute ‘extend’

AttributeError: ‘tuple’ object has no attribute ‘extend’

Error demo

1 In [5]: a.extend([4,5])                                                         
2 ---------------------------------------------------------------------------
3 AttributeError                            Traceback (most recent call last)
4 <ipython-input-5-42b5da5e2956> in <module>
5 ----> 1 a.extend([4,5])
6 
7 AttributeError: 'tuple' object has no attribute 'extend'

Print the properties of the tuple type, and you can see that except for the built-in type, the tuple type has only two properties: count and index

Extend is a method of type list

 1 In [3]: dir(tuple)                                                              
 2 Out[3]: 
 3 ['__add__',
 4  '__class__',
 5  '__contains__',
 6  '__delattr__',
 7  '__dir__',
 8  '__doc__',
 9  '__eq__',
10  '__format__',
11  '__ge__',
12  '__getattribute__',
13  '__getitem__',
14  '__getnewargs__',
15  '__gt__',
16  '__hash__',
17  '__init__',
18  '__init_subclass__',
19  '__iter__',
20  '__le__',
21  '__len__',
22  '__lt__',
23  '__mul__',
24  '__ne__',
25  '__new__',
26  '__reduce__',
27  '__reduce_ex__',
28  '__repr__',
29  '__rmul__',
30  '__setattr__',
31  '__sizeof__',
32  '__str__',
33  '__subclasshook__',
34  'count',
35  'index']

Extend is a list + another list

It changes the length of the original list without generating a new one

So if you use a = list. Extend (xxx), it doesn’t return a list as you would like, but a none

Example:

 1 In [6]: b=[1,2]                                                                 
 2 
 3 In [7]: c=b.extend([3])                                                         
 4 
 5 In [8]: b                                                                       
 6 Out[8]: [1, 2, 3] 
 9 
10 In [10]: print(c)                                                               
11 None
12 
13 In [11]: type(c)                                                                
14 Out[11]: NoneType

By the way:

Count indicates the number of occurrences of one element specified in the tuple

1 In [20]: b=(2,2,2,3,4)                                                          
2 
3 In [21]: b.count(2)                                                             
4 Out[21]: 3

Index returns the first occurrence position (subscript) of the specified element

1 In [20]: b=(2,2,2,3,4)                                                          
2 
3 In [22]: b.index(3)                                                             
4 Out[22]: 3
5 
6 In [23]: b.index(2)                                                             
7 Out[23]: 0

Attached:

All the properties of the list are as follows

 1 In [4]: dir(list)                                                               
 2 Out[4]: 
 3 ['__add__',
 4  '__class__',
 5  '__contains__',
 6  '__delattr__',
 7  '__delitem__',
 8  '__dir__',
 9  '__doc__',
10  '__eq__',
11  '__format__',
12  '__ge__',
13  '__getattribute__',
14  '__getitem__',
15  '__gt__',
16  '__hash__',
17  '__iadd__',
18  '__imul__',
19  '__init__',
20  '__init_subclass__',
21  '__iter__',
22  '__le__',
23  '__len__',
24  '__lt__',
25  '__mul__',
26  '__ne__',
27  '__new__',
28  '__reduce__',
29  '__reduce_ex__',
30  '__repr__',
31  '__reversed__',
32  '__rmul__',
33  '__setattr__',
34  '__setitem__',
35  '__sizeof__',
36  '__str__',
37  '__subclasshook__',
38  'append',
39  'clear',
40  'copy',
41  'count',
42  'extend',
43  'index',
44  'insert',
45  'pop',
46  'remove',
47  'reverse',
48  'sort']

: