Mark As Completed Discussion

Lists

So far, you've seen variables in Python. The problem with variables is that they can only store one value of a single data type.

Lists solve this problem for us. They can store a collection of data-- that is, multiple values of the same or different data types in a single variable.

Lists in Python are defined with square brackets. Let's look at some examples of lists.

What if we want to access any of its elements? This is done using indexing. In indexing, each value in a list is given a number (starting from 0, not 1!). To access a certain element, this number is specified inside the square bracket with the list name. For example, "w" from the list below can be accessed using lst[5].

Lists

Lists are very powerful, and many useful operations are defined with them. These list methods make it easier to manipulate and perform operations on lists. Some of these common methods are in the table below.

FunctionUsage
len()Get the length of list
append()Add more elements to an existing list
count()Gives the number of times a specified element appears in a list
pop()Remove the element at last index in the list
sort()Sort all the elements in the list according to specified order
copy()Copy all elements from one list to another, and returns the copy
JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment