List

Mutable, ordered series, traditionally of the same type of object.

Advantages: Mutable and ordered. Easy to understand. Relatively efficient memory usage.

Disadvantages: Searching is O(n).

Creating a list use square brackets

mylist = [ ]
mylist = [1,2,3]
mylist = [1,2,3,"a"]
mylist = ['a', 'b', 'c', [1,2,3] ]    # 4 elements

Checking membership

3 in mylist    # True or False
               #  O(n) time complexity
mylist.index(3)  # get the index of a element

Converting from another type

list('abc')      # ['a','b','c']
list((1,2,3))    # [1,2,3]
list({1,2,3})    # [1,2,3]

Slicing

A = [0, 1, 2, 3, 4, 5]
A[2:4]      # [2, 3]
A[2: ]      # [2, 3, 4, 5]
A[ :4]      # [0, 1, 2, 3]
A[ :-1]     # [0, 1, 2, 3, 4]
A[-3: ]     # [3, 4, 5]
A[-3:-1]    # [3, 4]
A[1:5:2]    # [1, 3]
A[5:1:-2]   # [5, 3]
# reverse a list
A[ : :-1]   # [5, 4, 3, 2, 1, 0]
# rotate a list
A[3: ] + A[ :3]    # [3, 4, 5, 0, 1, 2]
# shallow copy a list
B = A[:]

Replacing an existing element

mylist = ['a', 'b', 'c']
mylist[1] = 'z'
mylist           # ['a', 'z', 'c']

Replacing multiple existing elements

mylist = ['a', 'b', 'c', 'd', 'e', 'f']
mylist[1:3] = 'xyz'     # replace indexes 1 and 2 with x, y, z
mylist # ['a', 'x', 'y', 'z', 'd', 'e', 'f']

Adding an element to the end

# 1st way
mylist = ['a', 'b', 'c']
mylist.append('d')
mylist                  # ['a', 'b', 'c', 'd']
mylist.append([1,2,3])
mylist                  # ['a', 'b', 'c', 'd', [1,2,3]]

# 2nd way
mylist = ['a', 'b', 'c']
mylist += ['d']
mylist                  # ['a', 'b', 'c', 'd']

Adding multiple elements to the end

# 1st way
mylist = ['a', 'b', 'c']
mylist.extend([1,2,3])
mylist                  # ['a', 'b', 'c', 'd', 1, 2, 3]

# 2nd way
mylist = ['a', 'b', 'c']
mylist += [1,2,3]
mylist                  # ['a', 'b', 'c', 'd', 1, 2, 3]

Insert a element into a list at specific index

# 1st way: use insert() function
mylist = ['a', 'b', 'c']
mylist.insert(1, 'e')
mylist               # ['a', 'e', 'b', 'c']

# 2nd way: use slicing
mylist = ['a', 'b', 'c']
mylist[1:1] = 'e'
mylist               # ['a', 'e', 'b', 'c']

Removing an element from the end

mylist = ['a', 'b', 'c']
mylist.pop()          # returns 'c'
mylist                # ['a', 'b']

Removing an element from any index

# 1st way
mylist = ['a', 'b', 'c']
mylist.pop(0)        # returns 'a'
mylist               # ['b', 'c']

# 2nd way
mylist = ['a', 'b', 'c']
del mylist[0]        # ['b', 'c']

Removing an element based on its value (rather than its position)

mylist = ['a', 'b', 'c', 'a', 'a', 'b']
mylist.remove('a')     # Remove the first 'a'
mylist                 # ['b', 'c', 'a', 'a', 'b']

Removing a slice

mylist = ['a', 'b', 'c']
del mylist[0:1]      # ['c']

Sorting

# 1st way: in-place
mylist = ['d', 'a', 'c', 'b']
mylist.sort()          # Returns None
mylist                 # ['a', 'b', 'c', 'd']

# 2nd way
mylist = ['d', 'a', 'c', 'b']
mylist = sorted(mylist)
mylist                 # ['a', 'b', 'c', 'd']

Reversing

# 1st way: in-place
mylist = ['a', 'b', 'c']
mylist.reverse()      # returns None
mylist                # ['c', 'b', 'a']

# 2nd way
mylist = ['a', 'b', 'c']
mylist = list(reversed(mylist))
mylist                # ['c', 'b', 'a']

# 3rd way
mylist = ['a', 'b', 'c']
mylist = mylist[::-1]
mylist                # ['c', 'b', 'a']

Joining strings with delimiter

mylist = ['a', 'b', 'c']
'*'.join(mylist)     # 'a*b*c'
'...'.join(mylist)   # 'a...b...c'

Iterating over the elements

# 1st way
mylist = ['a', 'b', 'c']
for item in mylist:
    print(item)

# 2nd way
mylist = ['a', 'b', 'c']
for i in range(len(mylist)):
    print(mylist[i])

Iterating over the sorted elements

mylist = ['d', 'a', 'c', 'b']
for item in sorted(mylist):
    print(item)

Computing the length of a list

mylist = ['d', 'a', 'c', 'b']
len(mylist)     # 4

Find max/min value in a list

mylist = [1, 2.33]
max(mylist)   #2.33
min(mylist)   #1

Last updated