Python Lists

Python Lists

June 14, 2018
6 minutes read time
1 likes
0 comments
307 times viewed


header image

Description

This article will guide you about the use of lists in python. It will explain various functionalities and builtin functions associated with it.

Content

List

Basic List Operations

Concatenation

>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]

Repetition

>>> ["kode", ]*4
['kode', 'kode', 'kode', 'kode']

Membership

>>> 3 in [1, 2, 3, 4, 5]
True
>>> 7 in [1, 2, 3, 4, 5]
False
>>> 7 not in [1, 2, 3, 4, 5]
True

Indexing

>>> T=['C++', 'Java', 'Python']
>>> T[2]
'Python'
>>> T[-2]
'Java'

Slicing

>>> T = [1, 2, 4, 7, 3, 9]
>>> T[2:4]
[4, 7
>>> T[2:]
[4, 7, 3, 9]
>>> T[:2]
[1, 2]
>>> T[:]
[1, 2, 4, 7, 3, 9]

List Builtin Functions

len(list)

Gives the total length of the list.

>>> list1, list2 = [123, 'xyz', 'zara'], [456, 'abc']
>>> len(list1)
3
>>> len(list2)
2

max(list)

Returns item from the list with max value.

>>> list1, list2 = ['maths', 'che', 'phy', 'bio'], [456, 700, 200]
>>> max(list1)
'phy'
>>> max(list2)
700

min(list)

Returns item from the list with min value

>>> list1, list2 = ['maths', 'che', 'phy', 'bio'], [456, 700, 200]
>>> min(list1)
'bio'
>>> min(list2)
200

list(seq)

Converts seq to a list.

>>> tuple1= ('maths', 'che', 'phy', 'bio')
>>> list(tuple1)
['maths', 'che', 'phy', 'bio']
>>> list("astik")
['a', 's', 't', 'i', 'k']

List Builtin Methods

list.append(obj)

Appends object obj to list

>>> list1 = ['C++', 'Java', 'Python']
>>> list1.append('C#')
>>> list1
['C++', 'Java', 'Python', 'C#']

list.count(obj)

Returns count of how many times obj occurs in list

>>> aList = [123, 'xyz', 'zara', 'abc', 123]
>>> aList.count(123)
2
>>> aList.count('zara')
1
>>

list.extend(seq)

Appends the contents of seq to list

>>> list1 = ['physics', 'chemistry', 'maths']
>>> list2=list(range(5))
>>> list2
[0, 1, 2, 3, 4]
>>> list1.extend(list2)
>>> list1
['physics', 'chemistry', 'maths', 0, 1, 2, 3, 4]

Note: Same can be achieved using list1 = list1 + list2

list.index(obj)

Returns the lowest index in list that obj appears

>>> list1 = ['physics', 'chemistry', 'maths']
>>> list1.index('chemistry')
1
>>> list1.index('C#')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'C#' is not in list

list.insert(index, obj)

Inserts object obj into list at offset index

>>> list1 = ['physics', 'chemistry', 'maths']
>>> list1.insert(1, 'Biology')
>>> list1
['physics', 'Biology', 'chemistry', 'maths']

list.pop()

Removes and returns last object or obj from list

Note: If index given then pops the element at that position

>>> list1 = ['physics', 'Biology', 'chemistry', 'maths']
>>> list1.pop()
'maths'
>>> list1
['physics', 'Biology', 'chemistry']
>>> list1.pop(1)
'Biology'
>>> list1
['physics', 'chemistry']

list.remove(obj)

Removes object obj from list

>>> list1 = ['physics', 'Biology', 'chemistry', 'maths']
>>> list1.remove('Biology')
>>> list1
['physics', 'chemistry', 'maths']
>>> list1.remove('maths')
>>> list1
['physics', 'chemistry']

list.reverse()

Reverses objects of list in place

>>> list1 = ['physics', 'Biology', 'chemistry', 'maths']
>>> list1.reverse()
>>> list1
['maths', 'chemistry', 'Biology', 'physics']
>>> 

list.sort([func])

Sorts objects of list, use compare func if given

>>> list1 = ['physics', 'Biology', 'chemistry', 'maths']
>>> list1.sort()
>>> list1
['Biology', 'chemistry', 'maths', 'physics']

choice(seq)

Returns a random item from a list, tuple, or string.

>>> import random
>>> list1 = ['physics', 'Biology', 'chemistry', 'maths']
>>> random.choice(list1)
'Biology'
>>> random.choice(list1)
'chemistry'
>>> random.choice(range(100))
49
>>> random.choice(range(100))
60
>>> random.choice("astik")
'i'
>>> random.choice("astik")
'a'

*randrange ([start,] stop [,step])

returns a randomly selected element from range(start, stop, step).

import random
>>> random.randrange(50, 100, 2) # Random even number b/w 50 and 100
86
>>> random.randrange(50, 100, 2)
74

shuffle(lst)

Randomizes the items of a list in place.
Returns None.

>>> import random
>>> list1 = ['physics', 'Biology', 'chemistry', 'maths']
>>> random.shuffle(list1)
>>> list1
['maths', 'Biology', 'physics', 'chemistry']


We are done



Like this post

1   Like


Share this post on

Google Plus LinkedIn


About the author




Join the discussion

Nothing to preview

Post Comment



Comments