List

  • List is a collection of ordered items, where the items can be any data types
  • You can pack list of items by placing them into []
  • List is mutable

Creating List

Creating Empty List

In [32]:

empty = []      # literal assignment method
empty = list()  # constructor method
print (empty)
type(empty)
[]

Out[32]:

list

Creating List using Literal Assignment Method Multiple data types is allowed in a list

In [33]:

mylist = [123,'abc',456]

In [34]:

food = ['bread','noodle','rice','biscuit']

Creating List using Constructor Method Note that list(string) will split the string into letters

In [35]:

list('hello')

Out[35]:

['h', 'e', 'l', 'l', 'o']

Creating List using split() method Split base on spaces to create a list item

In [36]:

'a bunch of words'.split()

Out[36]:

['a', 'bunch', 'of', 'words']

Split doesn't break into items base on comma

In [37]:

'a1,a2,a3, a4'.split()

Out[37]:

['a1,a2,a3,', 'a4']

Accessing Items

Access specific index

In [38]:

food = list(['bread', 'noodle', 'rice', 'biscuit','jelly','cake'])
print (food[2])  # 3rd item
print (food[-1]) # last item
rice
cake

Access range of indexes

In [39]:

print (food[:4])   # first 3 items
print (food[-3:])  # last 3 items
print (food[1:5])  # item 1 to 4
print (food[5:2:-1]) # item 3 to 5, reverse order
print (food[::-1]) # reverse order
['bread', 'noodle', 'rice', 'biscuit']
['biscuit', 'jelly', 'cake']
['noodle', 'rice', 'biscuit', 'jelly']
['cake', 'jelly', 'biscuit']
['cake', 'jelly', 'biscuit', 'rice', 'noodle', 'bread']

Properties

Total Number of Items

In [40]:

len(food)

Out[40]:

6

Remove Item(s)

Search and remove first occurance of an item

In [41]:

food = list(['bread', 'noodle', 'rice', 'biscuit','jelly','cake'])
food.remove('noodle')
print (food)
['bread', 'rice', 'biscuit', 'jelly', 'cake']

Remove last item

In [42]:

food.pop()
print (food)
['bread', 'rice', 'biscuit', 'jelly']

Remove item at specific position

In [43]:

food.pop(1)
print(food)
['bread', 'biscuit', 'jelly']

Appending Item (s)

Append One Item

In [44]:

food.append('jelly')
print (food)
['bread', 'biscuit', 'jelly', 'jelly']

Append Multiple Items extend() will expand the list/tupple argument and append as multiple items

In [45]:

food.extend(['nand','puff'])
print (food)
['bread', 'biscuit', 'jelly', 'jelly', 'nand', 'puff']

Concateneting Multiple Lists

Concatenating Lists Although you can use '+' operator, however '-' operator is not supported

In [46]:

['dog','cat','horse'] + ['elephant','tiger'] + ['sheep']

Out[46]:

['dog', 'cat', 'horse', 'elephant', 'tiger', 'sheep']

Other Methods

Reversing the order of the items

In [47]:

food.reverse()
food

Out[47]:

['puff', 'nand', 'jelly', 'jelly', 'biscuit', 'bread']

Locating the Index Number of An Item

In [48]:

food.index('biscuit')

Out[48]:

4

Sorting The Order of Items

In [49]:

food.sort()
print (food)
['biscuit', 'bread', 'jelly', 'jelly', 'nand', 'puff']

List is Mutable

The reference list variable won't change after adding/removing its item

In [50]:

food = ['cake','jelly','roti','noodle']
print ('food : ',id(food))
food += ['salad','chicken']
print ('food : ',id(food))
food :  3036932581000
food :  3036932581000

In [51]:

x = [1,2,3]
y = [x,'abc']
print (y)
x[2] = 'k'
print (y)
[[1, 2, 3], 'abc']
[[1, 2, 'k'], 'abc']

Consider a function is an object, its variable (some_list) is immutable and hence its reference won't change, even data changes

In [52]:

def spam (elem, some_list=[]):
    some_list.append(elem)
    return some_list

print (spam(1))
print (spam(2))
print (spam(3))
[1]
[1, 2]
[1, 2, 3]

results matching ""

    No results matching ""