﻿ ﻿Working With Lists - Learn Python in One Day and Learn It Well (2015)

# Learn Python in One Day and Learn It Well (2015)

### Appendix B: Working With Lists

=> marks the start of the output

append( )

Add item to the end of a list

[Example]

myList = [‘a’, ‘b’, ‘c’, ‘d’]

myList.append(‘e’)

print (myList)

=> [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

del

Remove items from a list

[Example]

myList = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’]

#delete the third item (index = 2)

del myList

print (myList)

=> [‘a’, ‘b’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’]

#delete items from index 1 to 5-1

del myList[1:5]

print (myList)

=> [‘a’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’]

#delete items from index 0 to 3-1

del myList [ :3]

print (myList)

=> [‘i’, ‘j’, ‘k’, ‘l’]

#delete items from index 2 to end

del myList [2:]

print (myList)

=> [‘i’, ‘j’]

extend( )

Combine two lists

[Example]

myList = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

myList2 = [1, 2, 3, 4]

myList.extend(myList2)

print (myList)

=> [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, 1, 2, 3, 4]

In

Check if an item is in a list

[Example]

myList = [‘a’, ‘b’, ‘c’, ‘d’]

‘c’ in myList

=> True

‘e’ in myList

=> False

insert( )

Add item to a list at a particular position

[Example]

myList = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

myList.insert(1, ‘Hi’)

print (myList)

=> [‘a’, ‘Hi’, ‘b’, ‘c’, ‘d’, ‘e’]

len( )

Find the number of items in a list

[Example]

myList = [‘a’, ‘b’, ‘c’, ‘d’]

print (len(myList))

=> 4

pop( )

Get the value of an item and remove it from the list

Requires index of item as the parameter

[Example]

myList = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

#remove the third item

member = myList.pop(2)

print (member)

=> c

print (myList)

=> [‘a’, ‘b’, ‘d’, ‘e’]

#remove the last item

member = myList.pop( )

print (member)

=> e

print (myList)

=> [‘a’, ‘b’, ‘d’]

remove( )

Remove an item from a list. Requires the value of the item as the parameter.

[Example]

myList = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

#remove the item ‘c’

myList.remove(‘c’)

print (myList)

=> [‘a’, ‘b’, ‘d’, ‘e’]

reverse()

Reverse the items in a list

[Example]

myList = [1, 2, 3, 4]

myList.reverse()

print (myList)

=> [4, 3, 2, 1]

sort()

Sort a list alphabetically or numerically

[Example]

myList = [3, 0, -1, 4, 6]

myList.sort()

print(myList)

=> [-1, 0, 3, 4, 6]

sorted()

Return a new sorted list without sorting the original list.

Requires a list as the parameter

[Example]

myList = [3, 0, -1, 4, 6]

myList2 = sorted(myList)

#Original list is not sorted

print (myList)

=> [3, 0, -1, 4, 6]

#New list is sorted

print (myList2)

=> [-1, 0, 3, 4, 6]

Concatenate List

[Example]

myList = [‘a’, ‘b’, ‘c’, ‘d’]

print (myList + [‘e’, ‘f’])

=> [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]

print (myList)

=> [‘a’, ‘b’, ‘c’, ‘d’]

Multiplication Operator: *

Duplicate a list and concatenate it to the end of the list

[Example]

myList = [‘a’, ‘b’, ‘c’, ‘d’]

print (myList*3)

=> ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']

print (myList)

=> [‘a’, ‘b’, ‘c’, ‘d’]

Note:

The + and * symbols do not modify the list. The list stays as [‘a’, ‘b’, ‘c’, ‘d’] in both cases.

﻿