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.

Lists: Mutable & Dynamic

Give Feedback

In this lesson, you’ll explore how Python lists are both mutable and dynamic. Many types in Python are immutable. Integers, floats, strings, and (as you’ll learn later in this course) tuples are all immutable. Once one of these objects is created, it can’t be modified, unless you reassign the object to a new value.

The list is a data type that is mutable. Once a list has been created:

  • Elements can be modified.
  • Individual values can be replaced.
  • The order of elements can be changed.

Lists are also dynamic. Elements can be added and deleted from a list, allowing it to grow or shrink:

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

>>> a[2]
'bacon'
>>> a[2] = 10
>>> a
['spam', 'egg', 10, 'tomato', 'ham', 'lobster']
>>> a[-1] = 20
>>> a
['spam', 'egg', 10, 'tomato', 'ham', 20]

>>> s = 'mybacon'
>>> s[2]
'b'
>>> s[2] = 'f'
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    s[2] = 'f'
TypeError: 'str' object does not support item assignment

>>> a
['spam', 'egg', 10, 'tomato', 'ham', 20]
>>> del a[3]
>>> a
['spam', 'egg', 10, 'ham', 20]
>>> len(a)
5
>>> a[2:5] = [1.1, 2.2, 3.3]
>>> a
['spam', 'egg', 1.1, 2.2, 3.3]
>>> a[1:4]
>>> a[1:4] = ['Hello']
>>> a
['spam', 'Hello', 3.3]
>>> a[1:2] = ['a', 'b', 'c', 'd']
>>> a
['spam', 'a', 'b', 'c', 'd', 3.3]
>>> len(a)
6
>>> a[1]
'a'
>>> a[1] = [22, 33, 44]
>>> a
['spam', [22, 33, 44], 'b', 'c', 'd', 3.3]
>>> del a[1]
>>> a
['spam', 'b', 'c', 'd', 3.3]
>>> a[1:1] = [22, 33, 44]
>>> a
['spam', 22, 33, 44, 'b', 'c', 'd', 3.3]
>>> a[1:5]
[22, 33, 44, 'b']
>>> a[1:4] = []
>>> a
['spam', 'b', 'c', 'd', 3.3]

>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a += ['gravy', 'kiwi']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster', 'gravy', 'kiwi']
>>> a = [10, 20] + a
>>> a
[10, 20 'spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster', 'gravy', 'kiwi']

>>> a += 30
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    a += 30
TypeError: 'int' object is not iterable

>>> a += [30]
>>> a
[10, 20 'spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster', 'gravy', 'kiwi', 30]
>>> type([30])
<class 'list'>
>>> type(30)
<class 'int'>

>>> a = ['spam', 'egg', 'bacon']
>>> a
['spam', 'egg', 'bacon']
>>> a += 'tomato'
>>> a
['spam', 'egg', 'bacon', 't', 'o', 'm', 'a',' t', 'o']
>>> a[3:] = []
>>> a
['spam', 'egg', 'bacon']
>>> a += ['tomato']
>>> a
['spam', 'egg', 'bacon', 'tomato']

strings are immutable. so the code below does not work. s = ‘mybacon’ s[2] = ‘f’

what about code below? what are the differences? Thanks. s = ‘abc’ s = ‘abcdef’

Chris Bailey RP Team on Dec. 11, 2019

Hi DJ, In your first code example you are trying to mutate the string that is assigned to s, by accessing the third element (item) and changing it. Which will throw: TypeError: ‘str’ object does not support item assignment.

In your second code example you are reassigning the object s to a new value, which is possible. You aren’t changing the “string” but instead giving s a wholly different string. As python is a dynamically typed language you could even reassign s to an integer, or a some other type than a string. (s = 42) or s = [1, 2, 3].

Make sense. BTW: The tutorial is very help. Thank you, Chris.

Minh Pham on March 22, 2020

Hi Chris,

I like the way you teach this Python course and I am looking for similar course formats as offrened in RealPython, in other language as C# Java SQL, with the following criteria:

  • Iteractive
  • Video lecture & and Code in text
  • Quiz
  • Excercise
  • Small project

Can you recommend me some

Regards Minh

birajksahu on June 14, 2020

Hi Chris, Might sound trivial but I am not able to get the link about sort in description of the video. I am not sure where to locate the description of a video. BTW, loving the list and tuple video series :)

birajksahu on June 14, 2020

Please ignore got the answer myself :)

Become a Member to join the conversation.