0% found this document useful (0 votes)
17 views24 pages

Lists

Lists are mutable arrays that can contain elements of different types. They can be accessed using indexes to retrieve single or multiple elements. Various methods like append, insert, pop, remove etc. allow adding, modifying and removing elements. Lists can be sorted in ascending or descending order and common list operations like membership testing, concatenation, indexing and counting are supported.

Uploaded by

almulla7x
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views24 pages

Lists

Lists are mutable arrays that can contain elements of different types. They can be accessed using indexes to retrieve single or multiple elements. Various methods like append, insert, pop, remove etc. allow adding, modifying and removing elements. Lists can be sorted in ascending or descending order and common list operations like membership testing, concatenation, indexing and counting are supported.

Uploaded by

almulla7x
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Lists

• Lists are arrays


>>> x = [5,6,7]
>>> x
[5, 6, 7]

• They can contain different types


x = [1,'two',[1,2,3]]
>>> x
[1, 'two', [1, 2, 3]]
>>> len(x)
3
Lists
• Printing Lists
>>> x = [5,6,’ok’,7]
>>> print(x)
[5, 6, ok, 7]

• Accessing each element in list


>>> for w in x:
print(w)
5
6
Ok
7
Indexes, accessing single elements

>>> x = ['120','130','210','230’]
>>> x[0]
'120’
>>> x[-1]
'230’
>>> x[len(x)-1]
‘230’
>>> z=[1,3[,’ok’,[9,8,7]]
>>> z[3]
[9,8,7]
>>>z[3][1]
8
Indexes, accessing multiple elements

>>> x=['one','two','three','four']
>>> x[0:3]
['one', 'two', 'three']
>>> x[1:-1] #1st argument needs to be smaller than 2nd argument
['two', 'three']
Indexes, accessing multiple elements

>>> x=['one','two','three','four']
>>> x[:3] #from begin to target-1
['one', 'two', 'three']
>>> x[2:] #from target to end
['three’, ‘four’]
Reference to same list

>>> x=['one','two','three','four']
>>> y = x #y & x reference same list
>>> y[0]='ok’
>>> y
['ok', 'two', 'three', 'four']
>>> x
['ok', 'two', 'three', 'four']
Separate copy of list

>>> x=['one','two','three','four']
>>> w = x[:] #w gets a separate copy of x
>>> x
['one', 'two', 'three', 'four']
>>> w[0]='ok’
>>> w
['ok', 'two', 'three', 'four']
>>> x
['one', 'two', 'three', 'four’]
OR
>>> w2 = x.copy() # w2 has has a separate copy of x
Adding Single item to list

• Append single item to end of list


>>> x = [1,2,3,4]
>>> x.append(5)
>>> x
[1, 2, 3, 4, 5]
Or
>>> x = [1,2,3,4]
>>> x = x +[5] #list concat list
>>> x
[1, 2, 3, 4, 5]
Adding Single item to list
• Append single item to end of list
>>> x = [1,2,3,4]
>>> y = [5,6,7]
>>> x.append(y) #appends a single item
>>> x
[1, 2, 3, 4, [5,6,7] ]
Or
>>> x = [1,2,3,4]
>>> y = [5,6,7]
>>> x.extend(y) # extend takes in a list
>>> x
[1, 2, 3, 4, 5, 6, 7]
Adding Single item to list

• Append single item to begin of list


>>> x = [1,2,3,4]
>>> x.insert(0,-1) #add at index 0, the element -1
>>> x
[-1, 1, 2, 3, 4]

• Append single item anywhere in list


>>> x = [1,2,3,4]
>>> x.insert(2,’hello’)
>>> x
[ 1, 2, ‘hello’, 3, 4]
Adding Multiple items to list

• Append multiple items to end of list


>>> x = [1,2,3,4]
>>> x[len(x):]= [5,6,7]
>>> x
[1, 2, 3, 4, 5, 6, 7]

• Append multiple items to begin of list


>>> x[:0]=[-1,0]
>>> x
[-1, 0, 1, 2, 3, 4, 5, 6, 7]
Remove items from a list
• Removing multiple elements from list
>>> x = [1,2,3,4]
>>> x[1:-1]= []
>>> x
[1, 4]
• Removing multiple elements from list
>>> x = [1,2,3,4,5]
>>> del x[:2]
>>> x
[ 3, 4, 5]
• Removing all elements from list
>>> x = [1,2,3,4,5]
>>> x.clear()
Remove an item from a list

• Removing last elements from list


>>> x = [1,2,3,4]
>>> x.pop()
4
>>> x
[1, 2, 3]
• Removing any given elements from list
>>> x = [‘abc’,’234’,’xyz’]
>>> x.pop(1) #remove item at given index
>>> x
[‘abc’, ‘xyz’]
Remove an item from a list

• Removing any given element from list


>>> x = [1,2,3,4]
>>> del x[1] #remove data at given index
>>> x
[1, 3, 4]
• Removing any given element from list
>>> x = [‘abc’,’234’,’xyz’]
>>> x.remove(‘234’) #remove first occurrence of a given element from list
>>> x
[‘abc’,’xyz’]
Sorting list

>>> x = [9,7,8,4,1,5]
>>> x.sort()
>>> x
[1,4,5,7,8,9]

>>> y = [‘hi’, ‘how’, ‘are’, ‘you’ ]


>>> x.sort()
>>> x
[‘are’, ‘hi’, ‘how’, ‘you’]
Sorting list

>>> x = [9,’abc’,1,5]
>>> x.sort()
Error, cant sort with mixed types

>>> y = [[1,2], [0,2], [0,1], [1,1] ]


>>> x.sort()
>>> x
[ [0,1], [0,2], [1,1] , [1,2]]
Sorting list in reverse order

>>> x = [9,1,5]
>>> x.sort(reverse=True)
>>> x
[ 9 , 5, 1 ]
List membership

>>> x = [9,’abc’,1,5]
>>> 9 in x
True
>>> 7 in x
False
>>> 7 not in x #use of not
True
List concatenation

>>> x = [’abc’,1,5] + [8, 9]


>>> x
[‘abc’, 1, 5, 8, 9]
List search with index

>>> x = [‘ab’, ‘cde’, ‘xy’]


>>> x.index(‘cde’)
1
>>> x.index(‘abc’) # need to make sure argument exits else error
#runtime error
List matches with count

>>> x = [‘ab’, ‘cde’, ‘xy’]


>>> if x.count(‘cde’) > 0:
>>> x.index(‘cde’)
1
>>> if x.count(‘abcd’) > 0:
>>> x.index(‘cde’)
#nothing displayed
z = [1, 3, 'ok', [9, 8, 7]]
counter = 0
t = type(counter)
for r in z:
if type(r) == t:
print(counter, r, type(r))
counter += 1
z = [1, 3, 'ok', [9, 8, 7]]
counter = 0
t = type(z)
for r in z:
if type(r) == t:
print(counter, r, type(r))
for f in r:
print(f)
counter += 1
x = [1, 3, 'ok', [9, 8, 7],'ok','k']
x[len(x):]= [5,6,7]
print(x)
k = 'ok'

while (x.count(k) >0):


x.remove(k)
print(x)

You might also like