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.
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 With Return Values

Give Feedback

In this lesson, you’ll learn about the additional built-in methods that, unlike the ones in the previous lesson, have return values.

.pop(<index=-1>) removes an element from the list at the specified index. The item that was removed is returned. The default is to remove the last item in the list <index=-1> if no index is specified:

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

>>> b = a.pop()
>>> b
'ham'
>>> a
['spam', 'egg', 'bacon', 'tomato']
>>> a.pop(1)
'egg'
>>> a
['spam', 'bacon', 'tomato']
>>> a.pop(-2)
'bacon'
>>> a
['spam', 'tomato']

.index(<obj>[,<start>[, <end>]]) returns the first index of the value <obj> in the list. Optional start and end indexes can be used. It raises an exception if the value <obj> is not present:

>>>
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a.index('tomato')
3
>>> a[3]
'tomato'
>>> a.index('egg', 0, 4)
1
>>> a.index('egg', 2, 5)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    a.index('egg', 2, 5)
ValueError: 'egg' is not in list

.count(<obj>) returns the number of occurrences of the value <obj> in the list:

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

>>> b = [2, 4, 6, 8, 6, 4, 6, 2, 100]
>>> b
[2, 4, 6, 8, 6, 4, 6, 2, 100]
>>> b.count(6)
3
>>> b.count(0)
0
>>> b.count(77)
0

mylist.copy() returns a shallow copy of the list. For more details on shallow vs deep copying, check out Shallow vs Deep Copying of Python Objects:

>>>
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a.copy()
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> b = a.copy()
>>> a == b
True
>>> a is b
False
>>> a[0] = 'trees'
>>> a
['trees', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> b
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']

>>> x = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', ['hh', 'ii'], 'j']
>>> x
['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', ['hh', 'ii'], 'j']
>>> x[1]
['bb', ['ccc', 'ddd'], 'ee', 'ff']
>>> x[1][1]
['ccc', 'ddd']

>>> x
['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', ['hh', 'ii'], 'j']
>>> y = x.copy()
>>> y
['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', ['hh', 'ii'], 'j']
>>> x
['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', ['hh', 'ii'], 'j']
>>> y == x
True
>>> y is x
False
>>> y[1][1]
['ccc', 'ddd']
>>> y[1][2]
'ee'
>>> y[1][2] = 'zz'
>>> y
['a', ['bb', ['ccc', 'ddd'], 'zz', 'ff'], 'g', ['hh', 'ii'], 'j']
>>> x
['a', ['bb', ['ccc', 'ddd'], 'zz', 'ff'], 'g', ['hh', 'ii'], 'j']

>>> y[2] = 'Bob'
>>> y
['a', ['bb', ['ccc', 'ddd'], 'zz', 'ff'], 'Bob', ['hh', 'ii'], 'j']
>>> x
['a', ['bb', ['ccc', 'ddd'], 'zz', 'ff'], 'g', ['hh', 'ii'], 'j']

>>> z = x[:]
>>> z
['a', ['bb', ['ccc', 'ddd'], 'zz', 'ff'], 'g', ['hh', 'ii'], 'j']
>>> z[3][1]
'ii'
>>> z[3][1] = 'Jerry'
>>> z
['a', ['bb', ['ccc', 'ddd'], 'zz', 'ff'], 'g', ['hh', 'Jerry'], 'j']
>>> x
['a', ['bb', ['ccc', 'ddd'], 'zz', 'ff'], 'g', ['hh', 'Jerry'], 'j']

Comments & Discussion

manu on July 24, 2020

Hi can you update this airtical List and Tuples with following methods…?

mylist.index(<obj>[, <start>[, <end>]]) mylist.count(<obj>) mylist.copy() - returns a shallow copy

if possible shallow vs deep copy too. Thank you.

manu on July 24, 2020

Hi can you update this airtical List and Tuples with following methods…?

mylist.index(<obj>[, <start>[, <end>]]) 
mylist.count(<obj>) 
mylist.copy() - returns a shallow copy

if possible shallow vs deep copy too. Thank you.

Become a Member to join the conversation.