Loading video player…

Lists: Ordered & Arbitrary

You’ve already seen how to create a list. In this lesson, you’ll learn that lists are ordered and can contain a collection of arbitrary objects. The order used when defining a list is maintained for the lifetime of the list. Lists that contain the same elements, but in a different order, are not the same:

Python
>>> a = ['spam', 'egg', 'bacon', 'tomato']
>>> a
['spam', 'egg', 'bacon', 'tomato']

>>> b = ['egg', 'bacon', 'tomato', 'spam']
>>> b
['egg', 'bacon', 'tomato', 'spam']

>>> a == b
False
>>> a is b
False

>>> [1, 2, 3, 4] == [4, 1, 3, 2]
False

A list can contain arbitrary objects. The elements of a list can all be the same type or can contain any assortment of varying types. Lists can contain complex objects such as functions, classes, or modules:

Python
>>> a = [2, 4, 6, 8]
>>> a
[2, 4, 6, 8]
>>> type(a)
<class 'list'>

>>> a = [21.42, 'spam', 3, 4, 'egg', False, 3.14159]
>>> a
[21.42, 'spam', 3, 4, 'egg', False, 3.14159]
>>> type(a)
<class 'list'>

>>> int
<class 'int'>
>>> len
<built-in function len>

>>> def foo():
...    pass
...
>>> foo
<function foo at 0x108aacd90>

>>> import math
>>> math
<module 'math' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so'>

>>> a = [int, len, foo, math]
>>> a
[<class 'int'>, <built-in function len>, <function foo at 0x108aacd90>, <module 'math' from '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so'>]

A list can contain any number of objects, from zero, to as many as your computer’s memory will allow. A list with a single object is sometimes referred to as a singleton list:

Python
>>> a = []
>>> a
[]
>>> type(a)
<class 'list'>

>>> a = ['spam']
>>> a
['spam']
>>> type(a)
<class 'list'>

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ... 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, ... 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
>>> type(a)
<class 'list'>

The objects in a list don’t need to be unique. An object can appear multiple times within a list:

Python
>>> a = ['bark', 'meow', 'woof', 'bark', 'cheep', 'bark']
>>> a
['bark', 'meow', 'woof', 'bark', 'cheep', 'bark']
>>> type(a)
<class 'list'>

If you want to learn more about bpython, the REPL(Read–Eval–Print Loop) tool used in these videos, then you can check out these links:

00:00 In this video, you’ll learn how lists are ordered and can contain arbitrary objects. As was described in the previous video, a list is an ordered collection of objects.

00:11 The order that you use when defining a list is maintained for the lifetime of that list, and lists with the same elements in a different order are not the same. The order makes the list unique.

00:22 Let’s take a look at that. For the examples throughout this video course, I’m going to be using a custom REPL, so instead of just typing python3 and getting the standard REPL, I’m going to be typing bpython.

00:35 If you want to learn more about bpython, there’s information in the text below this video. So, lists are ordered, and if you created two lists

00:50 and those lists ended up having the same elements,

00:56 if you were to check if they were equal to each other, you’d see that that’s False. Or if you compared them as if a is b, that’d be False also. Even if you were to compare a simple list that has the same contents in each, but they’re in a different order, you’d see that that’s not True. Those are not equal to each other.

01:22 Those are unique. The order that objects are contained inside a list is an innate characteristic of that list. A list can also contain arbitrary objects. It’s not a requirement, the elements of lists can be of the same type, but the elements can be of varying types also.

01:38 So you can have integers and strings and even other lists contained within your list. And along with that, a list can even contain complex objects. Everything in Python is an object, so a list could contain objects such as functions or classes or even modules.

01:58 Let me have you explore that. Make a simple list of integers. Here’s a. Use the built-in function type() with a as its argument to check its type. So you can see that a is a list.

02:10 All the objects that are inside of a are integers, but a could contain very different objects. It could contain a float, a string, a couple of integers, another string, a Boolean, and another float.

02:29 And you can see them all in there. Is still a list? Yeah. Even an odd set of objects like an int or in this case, a function, or how about define a function instead of a built-in one.

02:46 This is going to be a very simple function. Even if you were to import a module, such as importing the math module. So, could you create a list that contained those types of objects? The int, the len built-in function, fu, and the math module?

03:05 Yeah, there they are. The <class 'int'>, the <built-in function len>, the <function foo> and the <module 'math'>.

03:14 And a list can contain any number of objects from zero to as many as your computer’s memory is going to allow. So a list that has only a single object is sometimes referred to as a singleton list. So, let me have you solidify some of that with a few examples. You can make a list that has no objects in it.

03:38 Is it still a list? Well, that checks out. It’s considered a list. And it’s still considered a list even if it just has a single object. That’s the one that’s sometimes referred to as a singleton list.

03:54 Or if it had a very large number of objects.

04:06 You can copy and paste this one in if you don’t want to type it. The code examples are below this video. But yeah, it’s still a list. The objects that are in a list, they don’t need to be unique. An object can appear multiple times within a list.

04:27 So here I’ll have you make a list that repeats the same string in it multiple times.

04:42 And it’s still a list.

04:46 Now that you’ve learned that lists are ordered and can contain arbitrary objects, next up, you’ll learn how to index and slice the contents of your lists.

Avatar image for JulianV

JulianV on Dec. 15, 2019

Thanks to let us know about bpython.

Avatar image for m3xipy

m3xipy on Feb. 15, 2020

prnt.sc/r2ltz2 right at the 28 second mark the video hangs if I manually move the player ahead to 30 seconds the video starts playing again.

Avatar image for Chris Bailey

Chris Bailey RP Team on Feb. 15, 2020

Hi @m3xipy, I’m trying to recreate the issue you were having. I have tested on a couple of computers and different browsers, and a mobile device. I’m not able to reproduce it. Thanks for the heads up, I will test again later. Are you having issues playing any of the other video lesson?

Avatar image for Sebastiaandb

Sebastiaandb on Feb. 11, 2024

Hi Chris, i’m using Visual Studio code and am just wondering how you are typing those lists that fast. I’m trying to follow along (split screen) and just typing all the lists by hand. Do you use any shortkey/ am i missing something or are you just really fast in typing those examples out?

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Feb. 12, 2024

@Sebastiaandb It looks like Chris is using bpython in his terminal, which provides powerful autocompletion features. Usually, hitting the TAB key or the right arrow key is enough to have it automatically fill the rest of the line based on the context. You can check out our tutorial on bpyton if you’d like to learn more about it.

Avatar image for Sebastiaandb

Sebastiaandb on Feb. 12, 2024

Thanks Bartosz!

Become a Member to join the conversation.