Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

List Methods

Give Feedback

In this lesson, you’ll learn about the built-in methods that you can use to modify lists. List methods are different from string methods. Because strings are immutable, the methods applied return a new string object. The list methods shown here modify the target list in place and don’t have a return value.

Here’s an example of a string method:

>>>
>>> s = 'mybacon'
>>> s.upper()
'MYBACON'
>>> s
'mybacon'
>>> t = s.upper()
>>> t
'MYBACON'
>>> s
'mybacon'

.append() appends an object to a list:

>>>
>>> a = ['a', 'b']
>>> a
['a', 'b']
>>> a.append(123)
>>> a
['a', 'b', 123]

>>> a = ['a', 'b']
>>> a
['a', 'b']
>>> x = a.append(123)
>>> x
>>> print(x)
None
>>> type(x)
>class 'NoneType'>
>>> a
['a', 'b', 123]

>>> a = ['a', 'b']
>>> a + [1, 2, 3]
['a', 'b', 1, 2, 3]
>>> a
['a', 'b']
>>> a.append([1, 2, 3])
>>> a
['a', 'b', [1, 2, 3]]

>>> a = ['a', 'b']
>>> a
['a', 'b']
>>> a.append('hello')
>>> a
['a', 'b', 'hello']

.extend() axtends a list by appending elements from the iterable:

>>>
>>> a = ['a', 'b']
>>> a
['a', 'b']
>>> a.extend([1, 2, 3])
>>> a
['a', 'b', 1, 2, 3]

>>> a = ['a', 'b']
>>> a
['a', 'b']
>>> a += [1, 2, 3]
>>> a
['a', 'b', 1, 2, 3]

.insert(<index>, <obj>) inserts the object <obj> into the list at the specified <index>, pushing the remaining list elements to the right:

>>>
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a.insert(3, 3.14159)
>>> a[3]
3.14159
>>> a
['spam', 'egg', 'bacon', 3.14159, 'tomato', 'ham', 'lobster']

.remove(<obj>) removes the first occurence of the value <obj> and raises a ValueError exception if the value is not present:

>>>
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a.remove('egg')
>>> a
['spam', 'bacon', 'tomato', 'ham', 'lobster']
>>> a.remove('egg')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    a.remove('egg')
ValueError: list.remove(x): x not in list

.clear() removes all items from the list:

>>>
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a.clear
>>> a
[]

.sort(<key=None>, <reverse=False>) sorts the list items in ascending order. An optional function can be used as a key. The optional reverse flag allows to reverse to descending order. For more details on this method, check out How to Use sorted() and sort() in Python.

Here’s an example:

>>>
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a.sort()
>>> a
['bacon', 'egg', 'ham', 'lobster', 'spam', 'tomato']

>>> a += ['Apple', 'Zebra']
>>> a
['bacon', 'egg', 'ham', 'lobster', 'spam', 'tomato', 'Apple', 'Zebra']
>>> a.sort()
>>> a
['Apple', 'Zebra', 'bacon', 'egg', 'ham', 'lobster', 'spam', 'tomato']
>>> a.sort(key=str.upper)
>>> a
['Apple', 'bacon', 'egg', 'ham', 'lobster', 'spam', 'tomato', 'Zebra']
>>> a.sort(key=str.upper, reverse=True)
>>> a
['Zebra', 'tomato', 'spam', 'lobster', 'ham', 'egg', 'bacon', 'Apple']


>>> b = [1, 77, 98, 34]
>>> b 
[1, 77, 98, 34]
>>> b.sort()
>>> b
[1, 34, 77, 98]
>>> b += ['apple']
>>> b
[1, 34, 77, 98, 'apple']
>>> b.sort()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    b.sort()
TypeError: '<' not supported between instances of 'str' and 'int'

.reverse() reverses the list in place:

>>>
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a.reverse()
>>> a
['lobster', 'ham', 'tomato', 'bacon', 'egg', 'spam']
>>> a.reverse()
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a[::-1]
['lobster', 'ham', 'tomato', 'bacon', 'egg', 'spam']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a = a[::-1]
>>> a
['lobster', 'ham', 'tomato', 'bacon', 'egg', 'spam']

kiran on July 24, 2020

a = [‘a’, ‘b’] a += ‘c’ #list + string a [‘a’, ‘b’, ‘c’]

a + = ‘c’ means a = a + ‘c’ but i can return like this it showing error

a = a + ‘d’ Traceback (most recent call last): File “<pyshell#40>”, line 1, in <module> a = a + ‘d’ TypeError: can only concatenate list (not “str”) to list

why like this?

kiran on July 24, 2020

>>> a = ['a', 'b']
>>> a += 'c'
>>> a
['a', 'b', 'c']
# a += 'c' means a = a + 'c' but i try like this a = a + 'd' it showing error.
>>> a = a + 'd'
Traceback (most recent call last):
  File "<pyshell#40>", line 1, in <module>
    a = a + 'd'
TypeError: can only concatenate list (not "str") to list

can you explain why it perform like this?

Chris Bailey RP Team on July 24, 2020

Hi @manupanduworld, The + operator will only concatenate lists together. It will not concatenate objects of different types. Hence the error you are getting. TypeError: can only concatenate list (not "str") to list

Two ways to accomplish the end result would be: Using concatenation

>>> a = ['a', 'b']
>>> a += ['c']       # adding a new list with the 'c' str object
>>> a
['a', 'b', 'c']

or using the .append() method

>>> a = ['a', 'b']
>>> a.append('c')  
>>> a
['a', 'b', 'c']

Become a Member to join the conversation.