Lists and Lists Operations in Python

A list in python is an ordered sequence of items or data. Python assumes lists as sequences which are preceded and ending with a square bracket.

Note that you cannot use the word list as a variable to reference a list because it is a reserved keyword

The following code snippets will help you to understand more about what lists are:

# Declare a variable to reference an empty list
# Method 1
our_list = []

# Method 2
our_list = list()
  • A list with elements: A list can contain a large number of elements. These elements can be of any data type. For instance, it can contain integers, floats, boolean values, other lists, strings etc

  • The following code snippets clarify this.

# A list with only integer elements.
our_list = [1,3,7,4,7,6,9,3,6,9]

# A list with several data types
second_list  = [23, True, ["Joseph", 4.7], "Nairobi"]

"""
The second_list variable defines a list that has an integer, boolean, another list, and a string.
"""
  • Indexing in lists Indexes in lists are basically positions that values are in in a list. Python indexing start from 0. The following code snippets explain about indexes:
second_list  = [23, True, ["Joseph", 4.7], "Nairobi"]

# To get the value of the item in the first position in the list:
print(second_list[0]) # Expected result is 23

print(second_list[3]) # Expected result is "Nairobi"

print(second_list[5]) # An IndexError will occur because the index passed is out of range

print(second_list[2]) # Expected output is ["Joseph", 4.7]

# To get the value of the Item in the second list, we have to pass the index of the list and the index of the value as follows: 
print(second_list[2][0]) # Expected output is "Joseph"
  • List slicing:

Taking into consideration a list of numbers from 0 to 10:

numbers = [0,1,2,3,4,5,6,7,8,9,10]
# In our list, the index is equal to the value
numbers[5] == 5 # True

# To get the values from the fourth position(index 3) to the seventh position(index 6) in a separate list
print(numbers[3:7]) # [3,4,5,6]

# To get all the even numbers in the list
print(numbers[::2]) # [0,2,4,6,8,10]

# To get all the odd numbers in the list
print(numbers[1::2]) # [1,3,5,7,9]

# To reverse the list
print(numbers[::-1]) # [10,9,8,7,6,5,4,3,2,1,0]

# How do negative indices relate to positive indices?
numbers[-1] == numbers[10] # True
numbers[-4] == numbers[7] # True

# Negative indices are therefore counted from  the last element beginning with -1
  • List operations, methods

List are considered objects in python. They therefore have methods that can be used to alter them. The following code snippets show how:

# Append
numbers = [0,1,2,3,4,5,6,7,8,9,10]
# append(value)
numbers.append(11) # The append() method adds the item passed at the end of the list
# Printing the list after the append operation has been carried on it: 
print(numbers) # [0,1,2,3,4,5,6,7,8,9,10, 11]

# Insert
# insert(index, value)
numbers.insert(12, 12) # The first argument is the index and the second argument is the value
print(numbers) # [0,1,2,3,4,5,6,7,8,9,10, 11, 12]

# finding the length of a list using the len() function
# len(iterable)
print(len(numbers)) #13

# remove and item from the list using the remove keyword
# remove(item)
numbers.remove(4) # Removes the item 4 from the list
print(numbers) # [0,1,2,3,5,6,7,8,9,10,11,12]

# The pop() function
# Used to remove the last element from the list. In case you want to remove a specific element, the element's index is passed in the list

numbers.pop()
print(numbers) # [0,1,2,3,5,6,7,8,9,10,11]

numbers.pop(4)
print(numbers) # [0,1,2,3,6,7,8,9,10,11]
  • Sum of all integers in the above list:
print(sum(numbers)) # 57
  • Concatenating two lists
"""Lists can be concatenated using the operand +"""

universities_a = ["JKUAT", "TUK", "KU", "UON"]
universities_b = ["Egerton", "Moi"]

print(universities_a + universities_b)  # ["JKUAT", "TUK", "KU", "UON", "Egerton", "Moi"]
  • These are just some of the basic lists operations
  • For more list operations, follow this blog and subscribe to it to receive updates when I post a new tutorial