Loading video player…

Creating a List

Resource linked in this lesson: When to Use a List Comprehension in Python

00:00 Let’s explore the three main ways you can create a new list. You can use list literals where you define a list as a sequence of comma-separated values and close with square brackets.

00:10 This has the syntax of my_list = [ item 0, item1, ..., , to indicate some items in the middle and ending in item_n]. Great for creating a list based on hard-coded values or specific variables.

00:25 Then there’s the list constructor, which can be called like a function. It uses the syntax. my_list = list(iterable). The word iterable is in brackets here to indicate that it’s actually optional.

00:40 If you call list() with no arguments at all, it returns an empty list. Perfect for creating a list based on another existing variable. And then a very Python way of creating a list, the list comprehension, sometimes called a list comp for short.

00:54 It may look similar to a literal, but there’s a lot more going on there. The syntax is my_list = and then all in square brackets, [expression for element in iterable].

01:06 The list comprehension iterates over some iterable and builds a new list based on the expression provided. You can add all sorts of logic to a comprehension, making it a very versatile and concise way to create a list.

01:19 Now let’s go to the REPL and see these techniques in action here in the REPL. Now let’s create some lists starting with list literals. How about a list of your favorite fruits?

01:30 fruits = ["pineapple", "coconut", "lime"]. Examine fruits and "pineapple", "coconut", "lime", just as written. You can make a list of Python built-ins.

01:44 functions = [print, len, range, type] Examine functions,

01:52 and you see the string representations of each item. And yep, range and type are technically classes, not functions. And of course lists are heterogeneous, so you’re free to make a list of completely different types as well.

02:05 Whatever random things come to mind. random_things = [ the integer 1701, the Python built-in type, the string "galaxy", and a tuple literal containing the strings ("will", "data")].

02:17 You can use type() to type check this and make sure that it is a list.

02:23 Yep, still a valid list though. This kind of heterogeneous collection is more common in the form of a database record and for efficiency reasons is typically better to represent those using tuples.

02:34 Sometimes you just want an empty list that will be filled programmatically later. empty = []

02:42 and empty is an empty list. This is equivalent to calling the list constructor with no arguments list() with a pair of empty parentheses.

02:49 Also returns an empty list. And speaking of the list constructor, it can also be used to turn an existing iterable into a list, but be careful. It will not create a list from multiple arguments.

03:00 So if you try calling list( and passing in the integers 0, 1, 2, 3, 4)

03:06 you get a TypeError: list expected at most, one argument and got five. What you need to do instead is use a valid iterable call list( passing in a tuple literal holding the integers, (0, 1, 2, 3, 4) and it returns a list with the same elements.

03:23 Lists can also consume set literals, passing the strings, {"pineapple", "coconut", "lime"} as a set literal to the list constructor. This time you get the list ["lime", "coconut", "pineapple"], and it’s important to note that sets are unordered so the result won’t necessarily have the same ordering that you used to create the set.

03:42 Another tricky one is passing a string. Since they’re also iterable. If you pass the string "Enterprise" to the list() constructor, the list() constructor breaks down the string into its individual characters resulting in a list of letters.

03:55 If this behavior catches you off guard, you can end up with some pretty subtle bugs. Another thing, because the list constructor consumes iterables, it’s great for creating lists out of objects that you might need to iterate over.

04:06 You’ll see more on iteration in future lessons, but for now, here’s how you can create a list of the integers 0 through 9 by using a range object call range( with the argument 10) and pass it to the list constructor

04:18 and there’s your list of numbers. Lastly, let’s look at comprehensions. At minimum you need an expression and an iterable, but simply an expression is anything that results in some value when evaluated. You can see this by using a list comprehension to create a list of zeros, [0 for _ in range(10)]

04:39 and you use an underscore here to show that the elements retrieved from the iterable go unused. For each of the 10 elements in the range object, you add a zero to the list and so you get a list of 10 zeros.

04:50 For a more typical example, you would normally use the value in the iterator in the expression like this [number ** 2 for number in range(1, 11)].

05:04 The double star here is the exponentiation operator and raises number to the power of two, because at each step of the loop number holds a value retrieved from the iterable.

05:14 This is done for all numbers from 1 to 11, not including 11, so your result, the squares of the numbers 1 through 10.

05:23 And this is just the tip of the iceberg on comprehensions. If you want to dive a bit deeper, have a look at “When to Use a List Comprehension in Python”.

05:32 Now you know about creating lists. Next up, actually accessing the elements inside them.

Become a Member to join the conversation.