Tips for Common Operations on Python Lists

Tips for Common Operations on Python Lists
Photo by Maxwell Nelson / Unsplash

1. Preface

In the previous article, we reviewed the common operations on Python tuples, in this article, let's talk about another common data type in Python: lists.

Like tuples, lists are also sequences created by square brackets []. The values in the list are commonly referred to as elements, and the data type of the elements can be different. For example, test_list = [0,1,1,'a','b'] can successfully create a list.

Unlike tuples, lists are mutable sequences, so the operations available for sequences will be more flexible. Next, we will review the most commonly used operations on lists.

2. Common operations on lists

2.1 Traversal

Taking the list people = ['Adam','Nick','Tony'] as an example, we can use a for loop to traverse:

for i in people:
  print(i)

When updating the list, using an index to traverse will be more convenient:

for i in range(len(people)):
  people[i] += '_suffix'

2.2 Slicing

Like tuples, you can get the elements of a list by slicing (the index in Python starts from 0):

print(people[:2])

Output:

['Adam','Nick']

2.3 Deleting Elements

a. The pop method can return the value to be deleted, and you can use an index to delete or delete the last element: a = people.pop(2) or a = people.pop(), the value of a will be 'Tony', and the original list will delete the corresponding element.

b. If you don't need the deleted value, del is a good method: del people[0], then the list people will become ['Nick','Tony'].

c. The remove method will delete the first required element in the list. For example, after the test_list.remove(1) operation, the list will become test_list = [0,1,'a','b']. Note that the remove method does not return any value.

2.4 Interaction between Lists and Strings

Lists and strings can be flexibly converted. For example, convert a string to a list:

# Convert string to list
name = 'Adam is very cool'
name_list_1 = list(name)
print(name_list_1)

# The split method can split the string using a delimiter
name_list_2 = name.split()
print(name_list_2)

Output:

['A', 'd', 'a', 'm', ' ', 'i', 's', ' ', 'v', 'e', 'r', 'y', ' ', 'c', 'o', 'o', 'l']

['Adam', 'is', 'very', 'cool']

Sometimes you need to merge a list into a string, the join method is very useful:

# Join the elements in the list with spaces into a new string
print(' '.join(['Adam', 'is', 'very', 'cool']))

Output:

'Adam is very cool'

2.5 Aliases of Lists

This is a point that is easy to make a mistake. In the following code, two variables are exactly the same, changing one will also change the value of the other:

# alias is an alias for the list name
name = ['Adam', 'is', 'very', 'cool']
alias = name
alias.pop()
print(name)

Output:

['Adam', 'is', 'very']

Therefore, when you want to use two independent lists, try to use separate assignment statements or shallow copies:

name = ['Adam', 'is', 'very', 'cool']

# Create a list with the same values
name_2 = ['Adam', 'is', 'very', 'cool']
# Shallow copy
name_3 = name[:]

2.6 List Arguments

Tuples can be passed into functions as parameters using the * operator, and lists can also be passed as parameters to functions. At this time, the function will get a reference to the list, which means that if the function modifies the list, the original list will also be modified. The following example demonstrates this:

# Define a function to delete the first element of a list
def del_head(t):
  del t[0]

name = ['Adam', 'is', 'very', 'cool']
del_head(name)
print(name)

Output:

['is', 'very', 'cool']

Parameter t and the variable name refer to the same list, so when t changes, name also changes. This knowledge point is very important. When writing functions, be very careful about whether the operation will create a new list or make changes to the original list.

2.7 Mapping and List Comprehensions

If you want to operate on a list with a function, mapping and list comprehensions are good choices. The built-in function map provided by Python takes two parameters - a mapping function and an iterable object, and returns an iterable object. The following example demonstrates this:

# Return the square of the element
def square(n):
    return n*n

test_list = [1,2,4,5]
result = map(square, test_list)
print(result)
print(list(result))

Output:

<map object at 0x1101b9400>
[1, 4, 16, 25]

You can also use list comprehensions to perform the same operation:

test_list = [1,2,4,5]
result = [i*i for i in test_list]
print(result)

Output:

[1, 4, 16, 25]

3. Summary

The above knowledge basically summarizes the common operations on lists. Here's a brief summary of the experience:

  • Most list methods modify the list in place and return None, so it's important to choose the appropriate method and ensure the safety of variables.
  • There are multiple ways to achieve the same operation, and sometimes using the wrong method may not result in an error, but the result might be incorrect. Make sure to understand the differences before choosing the appropriate method.
  • Be cautious when using aliases for lists. It's not just lists, other data types can also have aliasing mechanisms.

I hope these points have been helpful to you. Let's solidify our knowledge of lists with a LeetCode algorithm problem. Feel free to leave your answer in the comments! Next time, we'll talk about dictionaries (Dict).

Problem: Rotate Array

Given an array, rotate the array to the right by k steps, where k is a non-negative integer. Example:

Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
Rotate 1 step to the right: [7,1,2,3,4,5,6]
Rotate 2 steps to the right: [6,7,1,2,3,4,5]
Rotate 3 steps to the right: [5,6,7,1,2,3,4]

Author: LeetCode
Link: https://leetcode.com/leetbook/read/top-interview-questions-easy/x2skh7/
Source: LeetCode
Copyright: All rights reserved.