Data Structures Lists
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
Python List
If we store an integer and give it a name x. Then that's just one element right. There are often times when we need to store multiple elements and store them as one single variable name.
a = 4
print(a)4
list_a = [4, 4, 3, 5]
print(list_a)[4, 4, 3, 5]
As you've seen the syntax of creating a list is pretty simple. Just put together any number of elements inside the square brackets and give that square bracket a name.
b = [5, 6, 6]
print(b)[5, 6, 6]
We can store any data type inside a list
c = ["random string", 10, "what is this?", 10.2, True, False, None]
print(c)['random string', 10, 'what is this?', 10.2, True, False, None]
print(type(c))<class 'list'>
Subsetting / Indexing elements of a sequence
You might have figured out by now that list is a container of multiple elements and so I like to call it a sequence data type. We will talk about this topic on detail when we reach Numpy array. For now, just have the intuition that we can extract individual elements from sequence data types.
a1 = 10
a2 = 10.1
a3 = "Something"
a4 = True
a5 = False
a6 = None
list1 = [a1, a2, a3, a4, a5, a6, 700]
print(list1)[10, 10.1, 'Something', True, False, None, 700]
len(list1)6New ability unlocked: len() function Uses: calculate the length of a sequence
Now, let's learn some ways of accessing individual elements inside a sequence data type.
print(list1[0]) # This will give us the first element of our sequence10
Indexing is done using the square brackets. Indexing in python start from 0.
print(list1[1]) # This will give us second element10.1
If we want to access the last element of a sequence, we can use negative indexing of Python.
print(list1[-1]) # This will give us the last elementNone
print(list1[-2]) # Second last elementFalse
If our objective is to extract a range of elements, we could use the : operator.
# list_name[first_index (included): second_index (excluded)]print(list1[0:2]) # The first index is included,
# while the second index is excluded
# index 0 and index 1 elements will be returned[10, 10.1]
print(list1)[10, 10.1, 'Something', True, False, None]
print(list1[0:3])[10, 10.1, 'Something']
print(list1[0:4])[10, 10.1, 'Something', True]
print(list1)[10, 10.1, 'Something', True, False, None]
print(list1[1:3]) # second element (index 1) included,
# fourth element (index 3) excluded[10.1, 'Something']
print(list1[0:]) # print everything starting from index 0[10, 10.1, 'Something', True, False, None]
print(list1[1:]) # print everything starting from index 1[10.1, 'Something', True, False, None]
print(list1[3:]) # print everything starting from index 3[True, False, None]
# list_name[first_index (included): second_index (excluded) : step]list1[10, 10.1, 'Something', True, False, None]print(list1[0:3:2]) # start from index 0, go till index 2 in the steps of 2[10, 'Something']
list1[10, 10.1, 'Something', True, False, None]# print list starting from index 0 till index 3 with the step of 1
print(list1[0: 4: 1])[10, 10.1, 'Something', True]
# print list starting from index 0 till index 3 with the step of 2
print(list1[0: 4: 2])[10, 'Something']
range() function
This works on the concept of generators which we will cover later. For now, lets think of it as a sequence generator.
There exists a list function inside python which we can use to create lists. Lets use both range and list function to create a sequence.
set([11,1,11,1,11,1]){1, 11}list2 = list(range(10))
print(list2)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list2[0:][0, 1, 2, 3, 4, 5, 6, 7, 8, 9]list2[:-1] # Everything except the last element[0, 1, 2, 3, 4, 5, 6, 7, 8]List methods
There are some useful in-built functions that are available for the list data type. Let's look at some of them. You are not expected to cram any of these functions.
list2[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]append method
Just add a new element at the end of the list
list2 = list(range(10))
list2.append("new element")list2[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'new element']pop method
Remove an element from the list and return it to the user. Its actual working will be understood after the function chapter. So, till then, just stick with the easy explaination, that it removes an element from a list.
list2.pop(-1) # Remove the last element from the list2'new element'list2[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]list2.pop(0) # Remove the first element from the list20list2[1, 2, 3, 4, 5, 6, 7, 8, 9]We can make changes to the elements inside a list. Let's say we want to change the value of first element of list to 100.
list2 = list(range(10))
list2[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]list2[0]0list2[0] = 100 # Just use proper indexing to reference the element inside a list and then assign a new valuelist2[100, 1, 2, 3, 4, 5, 6, 7, 8, 9]Let's say we want to add 1000 to the last element. First, reference the last element and then add 1000 to it and assign this new value to the old reference (appropriate index)
list2[-1] = list2[-1] + 1000list2[100, 1, 2, 3, 4, 5, 6, 7, 8, 1009]Finished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →