Session1 - 2 (Lists)
Session1 - 2 (Lists)
A list can have any number of items and they may be of different types (integer, float, string, etc.).
a pair of brackets [ ] are used, and the elements within it are separated by commas.
In [33]:
list_1 = [1, 2, 3]
list_1
[1, 2, 3]
Out[33]:
In [34]:
#Lists are mutable
list_1[1]=4
print(list_1)
[1, 4, 3]
In [9]:
list_2 = ['Hello', 'World']
list_2
['Hello', 'World']
Out[9]:
In [10]:
list_3 = [1, 2, 3, 'Apple', 'orange']
list_3
In [11]:
#We can also nest the lists
list_4 = [list_1, list_2]
list_4
In [12]:
list_3[2]
3
Out[12]:
In [13]:
list_3[:2]
[1, 2]
Out[13]:
In [14]:
list_3[:3]
[1, 2, 3]
Out[14]:
In [15]:
list_3[-1]
'orange'
Out[15]:
In [16]: len(list_3)
5
Out[16]:
In [18]:
#concatenate
list_1 + list_2
In [19]:
list_1.append(4)
list_1
[1, 2, 3, 4]
Out[19]:
In [33]:
list_1=[1,2,3,4]
list_1
[1, 2, 3, 4]
Out[33]:
In [34]:
list_1.insert(2,'center')
list_1
[1, 2, 'center', 3, 4]
Out[34]:
In [35]:
list_1.insert(2,'left')
list_1
In [36]:
list_1.insert(4,'right')
list_1
In [37]:
del list_1[2]
list_1
In [38]:
del list_1[2]
list_1
[1, 2, 'right', 3, 4]
Out[38]:
In [39]:
del list_1[2]
list_1
Out[39]: [1, 2, 3, 4]
In [40]:
#Define an empty list and add values 5 and 6 to the list.
list_5 = []
list_5.append(5)
list_5
[5]
Out[40]:
In [41]:
list_5.append(6)
list_5
[5, 6]
Out[41]:
In [44]:
list_5[1]=2
list_5
[5, 2]
Out[44]:
In [45]:
list_5[2]=9
list_5
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_5716/886566665.py in <module>
----> 1 list_5[2]=9
2 list_5
In [46]:
5 in list_5
True
Out[46]:
In [65]:
list_7=[[1,2,3],[4,5,6],[7,8,9]]
list_7
In [66]:
list_7[0][0]
1
Out[66]:
In [67]:
list_7[0][1]
2
Out[67]:
In [68]:
list_7[0]
file:///C:/Users/GVPCOE/Desktop/Python sessions/Session1_2 (Lists).html 3/8
5/6/22, 9:53 AM Session1_2 (Lists)
[1, 2, 3]
Out[68]:
In [85]:
list_7[2][0:2]
[7, 8]
Out[85]:
In [71]:
list_7[:]
In [86]:
list_7[-1]
[7, 8, 9]
Out[86]:
In [91]:
list_7[-2:-1]
[[4, 5, 6]]
Out[91]:
In [92]:
list_7
In [93]:
list_7.append([10,11,12])
In [94]:
list_7
In [97]:
list_7[-3:-1]
In [100…
list_7[0][0:2]=[0,0]
list_7
In [101…
del list_7[3]
In [102…
list_7
In [103… list_7.clear()
In [104…
list_7
[]
Out[104…
In [105…
list_8=[1,2,3,4]
In [106…
list_8.remove(2)
In [107…
list_8
[1, 3, 4]
Out[107…
In [108…
list_8.index(3)
1
Out[108…
In [115…
l=[0]*4
l
[0, 0, 0, 0]
Out[115…
In [116…
l*4
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Out[116…
In [119…
l=[1,2,3]
l
[1, 2, 3]
Out[119…
In [120…
l*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
Out[120…
In [126…
t1=['a','b','c']
t2=['d','e']
t1.extend(t2)
print(t1)
In [124…
print(t2)
In [2]:
t = ['d', 'c', 'e', 'b', 'a']
t.sort()
print(t)
In [6]:
t1=t+[1,2,3]
print(t1)
In [133…
#pop modifies the list and returns the element that was removed.
t = ['a', 'b', 'c']
x = t.pop(1)
print(t)
print(x)
['a', 'c']
b
In [135…
# If you don’t provide an index in pop, it deletes and returns the last element.
t = ['a', 'b', 'c']
x = t.pop()
print(t)
print(x)
['a', 'b']
c
In [137…
t = ['a', 'b', 'c']
del t[1]
print(t)
['a', 'c']
In [138…
t = ['a', 'b', 'c']
t.remove('b')
print(t)
['a', 'c']
In [5]:
# convert string to list
s='spam'
t=list(s)
print(t)
In [7]:
# break strin into words
s='Hello World'
t=s.split()
print(t)
['Hello', 'World']
In [9]:
#You can call split with an optional argument called a delimiter
s='spam-spam-spam'
delimiter='-'
t=s.split(delimiter)
print(t)
In [14]:
t=['spam','spam','spam']
delimiter='-'
delimiter.join(t)
'spam-spam-spam'
Out[14]:
In [15]:
t=['spam','spam','spam']
delimiter=' '
delimiter.join(t)
In [16]:
t=['spam','spam','spam']
delimiter=''
delimiter.join(t)
'spamspamspam'
Out[16]:
In [17]:
list_10=["apple","banana","cherry"]
In [26]:
#Lists allow duplicate values:
list_10= ["apple", "banana", "cherry", "apple", "cherry"]
list_10
In [27]:
#If you insert more items than you replace, the new items will be inserted where you sp
list_10[1:2]=["blackcurrant","watermelon"]
print(list_10)
If you insert less items than you replace, the new items will be inserted where you specified, and the
remaining items will move accordingly:
In [28]:
thislist = ["apple", "banana", "cherry"]
thislist[1:3] = ["watermelon"]
print(thislist)
file:///C:/Users/GVPCOE/Desktop/Python sessions/Session1_2 (Lists).html 7/8
5/6/22, 9:53 AM Session1_2 (Lists)
['apple', 'watermelon']
In [4]:
list_10=['apple', 'blackcurrant', 'watermelon', 'cherry', 'apple', 'cherry']
list_10[1:4]=['gooseberry']
print(list_10)
In [6]:
list_10.index('apple')
0
Out[6]:
In [ ]: