Lists

3 minute read

Unlike Lists interface and primitive type arrays in Java, Python has only one such type for representing lists i.e. list.
Let’s dive in code directly.

# Ways to create list
names = list()    # this is preferred way

print(names)
>>> []

names = []
print(names)
>>> []

names = ['Jack', 'John', 'Harry', 'Ron', 'Jones', 'Davy']    # list with values in it
print(names)
>>> ['Jack', 'John', 'Harry', 'Ron', 'Jones', 'Davy']

# Size of list
print(len(names))
>>> 6

Accessing elements

names = ['Jack', 'John', 'Harry', 'Ron', 'Jones', 'Davy'] 
print(names)
>>> ['Jack', 'John', 'Harry', 'Ron', 'Jones', 'Davy']

# indexes are 0 based
print(names[0])
>>> Jack

# negative indexes are referred from end
print(names[-1])
>>> Davy

# non existing indexes raises IndexError
print(names[100])
>>> IndexError: list index out of range

IndexOf, Contains

names = ['Jack', 'John', 'Harry', 'Ron', 'Jones', 'Davy'] 
print(names)
>>> ['Jack', 'John', 'Harry', 'Ron', 'Jones', 'Davy']

# IndexOf
print(names.index('Jack'))
>>> 0

# IndexOf some value which is not present in list raises ValueError
print(names.index('X'))
>>> ValueError: 'X' is not in list

# Contains check
print('John' in names)
>>> True

print('X' in names)
>>> False

Add, Remove Operations

names = ['Jack', 'John', 'Harry', 'Ron', 'Jones', 'Davy'] 
print(names)
>>> ['Jack', 'John', 'Harry', 'Ron', 'Jones', 'Davy']

# Adding an element
names.append('Xavi')
print(names)
>>> ['Jack', 'John', 'Harry', 'Ron', 'Jones', 'Davy', 'Xavi']

# Removing an element
names.remove('Xavi')
print(names)
>>> ['Jack', 'John', 'Harry', 'Ron', 'Jones', 'Davy']

# Only one instance is removed at once
names.append('Xavi')
names.append('Xavi')
names.append('Xavi')
print(names)
>>> ['Jack', 'John', 'Harry', 'Ron', 'Jones', 'Davy', 'Xavi', 'Xavi', 'Xavi']

names.remove('Xavi')
print(names)
>>> ['Jack', 'John', 'Harry', 'Ron', 'Jones', 'Davy', 'Xavi', 'Xavi']

Sorting, Reversing

names = ['Jack', 'John', 'Harry', 'Ron', 'Jones', 'Davy']

# Doesn't affect original list, a new list is returned
print(sorted(names))
>>> ['Davy', 'Harry', 'Jack', 'John', 'Jones', 'Ron']

print(sorted(names) is names)
>>> False

print(sorted(names) == names)
>>> False

# Sorting in reverse order
print(sorted(names, reverse=True))
>>> ['Ron', 'Jones', 'John', 'Jack', 'Harry', 'Davy']

# Sorting original list
names.sort()
print(names)
>>> ['Davy', 'Harry', 'Jack', 'John', 'Jones', 'Ron']

# Reversing list
print(list(reversed(names)))
>>> ['Ron', 'Jones', 'John', 'Jack', 'Harry', 'Davy']

Interaction with other lists

letters = ['A', 'B', 'C', 'D', 'E', 'F']
new_letters = ['X', 'Y', 'Z']
letters.extend(new_letters)
print(letters)
>>> ['A', 'B', 'C', 'D', 'E', 'F', 'X', 'Y', 'Z']

letters = ['A', 'B', 'C', 'D', 'E', 'F']
new_letters = ['X', 'Y', 'Z']
tmp_list = letters + new_letters
print(tmp_list)
>>> ['A', 'B', 'C', 'D', 'E', 'F', 'X', 'Y', 'Z']

Slicing

Slicing takes three parameters: start index, exclusive end index, and skip parameter

letters = ['A', 'B', 'C', 'D', 'E', 'F']

# A new list same as letters
prints(letters[:])
>>> ['A', 'B', 'C', 'D', 'E', 'F']

# A new list with elements skipped 
print(letters[::2])
>>> ['A', 'C', 'E']

# Sublist, takes elements from index 1 to 3(exclusive)
print(letters[1:3])
>>> ['B', 'C']

# Negative indexes work too
print(letters[1:-1]
>>> ['A', 'B', 'C', 'D', 'E']

Iterating over list

>>> list_1 = [1, 2, 3, 4, 5, 6]

for x in list_1:
    print(x, end =' ')
>>> 1 2 3 4 5 6

for i, x in enumerate(list_1):
    print('index: '+ str(i) + ' value: ' + str(x))
>>> index: 0 value: 1
... index: 1 value: 2
... index: 2 value: 3
... index: 3 value: 4
... index: 4 value: 5
... index: 5 value: 6

Stack

stack = list()
# Appends add elements at last
stack.append(1)
stack.append(2)
stack.append(3)

print(stack)
>>> [1, 2, 3]

# Pops the last element
stack.pop()

print(stack)
>>> [1, 2]

## Slicing way

stack = list()

stack.append(1)
stack.append(2)
stack.append(3)

print(stack)
>>> [1, 2, 3]

# Tuple construction
(item, stack) = stack[-1], stack[:-1]

print(item)
>>> 3
print(stack)
>>> [1, 2]

Queue

queue = list()
queue.append(1)
queue.append(2)
queue.append(3)

print(queue)
>>> [1, 2, 3]

# Pop takes index as well, this time its FIFO
queue.pop(0)
print(queue)
>>> [2, 3]

queue.pop(0)
print(queue)
>>> [3]

queue.pop(0)
print(queue)
>>> []

## Slicing way

queue = list()
queue.append(1)
queue.append(2)
queue.append(3)

print(queue)
>>> [1, 2, 3]

item, queue = queue[0], queue[1:]

print(item)
>>> 1

print(queue)
>>> [1, 2, 3]

Comments