Imagine you received many music recommendations from your friends and compiled all of the recommendations into a table, with specific information about each movie.
The table has one row for each album and several columns:
The dataset can be seen below:
<table font-size:xx-small style="width:25%"> Artist Album Released Length Genre Music recording sales (millions) Claimed sales (millions) Released Soundtrack Rating (friends) Michael Jackson Thriller 1982 00:42:19 Pop, rock, R&B 46 65 30-Nov-82 10.0 AC/DC Back in Black 1980 00:42:11 Hard rock 26.1 50 25-Jul-80 8.5 Pink Floyd The Dark Side of the Moon 1973 00:42:49 Progressive rock 24.2 45 01-Mar-73 9.5 Whitney Houston The Bodyguard 1992 00:57:44 Soundtrack/R&B, soul, pop 26.1 50 25-Jul-80 Y 7.0 Meat Loaf Bat Out of Hell 1977 00:46:33 Hard rock, progressive rock 20.6 43 21-Oct-77 7.0 Eagles Their Greatest Hits (1971-1975) 1976 00:43:08 Rock, soft rock, folk rock 32.2 42 17-Feb-76 9.5 Bee Gees Saturday Night Fever 1977 1:15:54 Disco 20.6 40 15-Nov-77 Y 9.0 Fleetwood Mac Rumours 1977 00:40:01 Soft rock 27.9 40 04-Feb-77 9.5 </table></font>
To create a list, type the list within square brackets [ ], with your content inside the parenthesis and separated by commas. Let’s try it!
In [1]:
L = ["Michael Jackson" , 10.1,1982]
L
Out[1]:
We can use negative and regular indexing with a list :
In [2]:
print('the same element using negative and positive indexing:\n Postive:',L[0],
'\n Negative:' , L[-3] )
print('the same element using negative and positive indexing:\n Postive:',L[1],
'\n Negative:' , L[-2] )
print('the same element using negative and positive indexing:\n Postive:',L[2],
'\n Negative:' , L[-1] )
Lists can contain strings, floats, and integers. We can nest other lists, and we can also nest tuples and other data structures. The same indexing conventions apply for nesting:
In [11]:
temp = [ "Michael Jackson", 10.1,1982,[1,2],("A",1) ]
for i in range(0, len(temp)):
print(temp[i], temp[-len(temp)+i])
We can also perform slicing in lists. For example, if we want the last two elements, we use the following command:
In [12]:
L = [ "Michael Jackson", 10.1,1982,"MJ",1]
L
Out[12]:
In [13]:
L[3:5]
Out[13]:
We can use the method "extend" to add new elements to the list:
In [14]:
L = [ "Michael Jackson", 10.2]
L.extend(['pop',10])
L
Out[14]:
Another similar method is 'appended'. If we apply 'appended' instead of 'extended', we add one element to the list:
In [15]:
L = [ "Michael Jackson", 10.2]
L.append(['pop',10])
L
Out[15]:
Each time we apply a method, the list changes. If we apply "extend" we add two new elements to the list. The list L is then modified by adding two new elements:
In [16]:
L = [ "Michael Jackson", 10.2]
L.extend(['pop',10])
L
Out[16]:
If we append the list ['a','b'] we have one new element consisting of a nested list:
In [17]:
L.append(['a','b'])
L
Out[17]:
As lists are mutable, we can change them. For example, we can change the first element as follows:
In [18]:
A = ["disco",10,1.2]
print('Before change:', A)
A[0] = 'hard rock'
print('After change:', A)
We can also delete an element of a list using the del command:
In [19]:
print('Before change:', A)
del(A[0])
print('After change:', A)
We can convert a string to a list using 'split'. For example, the method split translates every group of characters separated by a space into an element in a list:
In [20]:
'hard rock'.split()
Out[20]:
We can use the split function to separate strings on a specific character. We pass the character we would like to split on into the argument, which in this case is a comma. The result is a list, and each element corresponds to a set of characters that have been separated by a comma:
In [21]:
'A,B,C,D'.split(',')
Out[21]:
When we set one variable B equal to A; both A and B are referencing the same list in memory :
In [22]:
A = ["hard rock",10,1.2]
B = A
print('A:', A)
print('B:', B)
Initially, the value of the first element in B is set as hard rock. If we change the first element in A to 'banana', we get an unexpected side effect. As A and B are referencing the same list, if we change list A, then list B also changes. If we check the first element of B we get banana instead of hard rock:
In [23]:
print('Before changing A[0], B[0] is ',B[0])
A[0] = "banana"
print('After changing A[0], A[0] is ',A[0])
print('After changing A[0], B[0] is ',B[0])
This is demonstrated in the following figure:
You can clone list A by using the following syntax:
In [24]:
B = A[:]
B
Out[24]:
Variable B references a new copy or clone of the original list; this is demonstrated in the following figure:
Now if you change A, B will not change:
In [25]:
print('Before changing A[0], B[0] is ',B[0])
A[0] = "apple"
print('After changing A[0], A[0] is ',A[0])
print('After changing A[0], B[0] is ',B[0])
In [ ]:
In [26]:
a_list = [1, "hello", [1, 2, 3], True]
a_list
Out[26]:
In [27]:
a_list[1]
Out[27]:
In [28]:
a_list[1:3]
Out[28]:
a_list[1:3]
In [32]:
A = [1, 'a']
print(A)
B = [2, 1, 'd']
print(B)
print(A + B)
A=[1,'a']
B=[2,1,'d']
A+B
Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact ]human cognition. Joseph has been working for IBM since he completed his PhD.
In [ ]: