Introduction to [Python Foundation Series] List

Keywords: Python list

#Introduction to [Python Foundation Series] List

List is one of the most commonly used data structures in Python. Its features include:

  • Ordered, can be traversed by index

  • Store multiple data structures

Some characteristics

Create array

To create an array, you can use [], such as:

test_list = ["Jon Snow", "Winterfell", 30]

print(jon_snow) # ['Jon Snow', 'Winterfell', 30]

print(jon_snow[0]) # Jon Snow

print(len(jon_snow)) # 3

Or pass an iteratable into the list() function, such as:

num_seq = range(0, 10)  # 0-9 sequence, the result of range is an iterative object
num_list = list(num_seq)
print(num_list) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Array nesting (multiple arrays)

Arrays can store multiple data structures and naturally include arrays:

list_of_list = [[1,2,3],[4,5,6],[7,8,9]]
print(list_of_list) # [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Similarly, multiple arrays can be accessed using indexes:

list_of_list = [[1,2,3],[4,5,6],[7,8,9]]
print(list_of_list[1]) # [4, 5, 6]
print(list_of_list[1][1])  # 5

Merge array

In Python, you can directly use the + operator to merge arrays. This method will not modify the original array:

part_A = [1, 2, 3, 4, 5]
part_B = [6, 7, 8, 9, 10]
merged_list = part_A + part_B
print(merged_list) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

You can also use extend() to splice arrays, which will modify the original array:

part_A = [1, 2, 3, 4, 5]
part_B = [6, 7, 8, 9, 10]
part_A.extend(part_B)
print(part_A) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Common array operations

increase

Directly add a new value at the end of the array. You can use append:

num_list = []
num_list.append(1)
num_list.append(2)
num_list.append(3)
print(num_list) # [1, 2, 3]

To add a value to a specific index, you can use insert(index, element) to operate. Using insert(index, element) will move the original index and its subsequent values backward by 1 bit:

num_list = [1, 2, 3, 5, 6]
num_list.insert(3, 4)
print(num_list) # [1, 2, 3, 4, 5, 6], 5 and 6 move backward by one bit

Delete

pop() will delete the last value in the list:

test_list = [1, 2, 3, 4, 5]
popped = test_list.pop()

print(popped) # 5
print(test_list) # [1, 2, 3, 4]

Using remove(value) deletes the value specified in the parameter. Note that if there are duplicate values in the list, remove(value) will only delete the first value, not all values:

test_list = [1,2,3,4,5,1]
test_list.remove(1)

print(test_list) # [2, 3, 4, 5, 1]

change

Modifying the value of the list can be done directly through the index:

test_list = [1,2,3,4,5,5]
test_list[5] = 6

print(test_list) # [1, 2, 3, 4, 5, 6]

check

If you just want to know whether there are some values in the List, you can use the in keyword:

nums = [1,2,3,4,5,6,7,8]
1 in nums # True

To find an index, you can use the index() method:

nums.index(4) # 3

nums.index(10)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 10 is not in list

List slicing

The method of List Slicing is similar to that of intercepting strings:

nums = [1,2,3,4,5,6,7,8]
print(nums[0:5]) # [1, 2, 3, 4, 5]
print(nums[::-1]) # [8, 7, 6, 5, 4, 3, 2, 1]

sort

Using sort() will modify the original array:

unsorted_list = [1,412,53,6,2,6,8,234,59]
unsorted_list.sort()
unsorted_list # [1, 2, 6, 6, 8, 53, 59, 234, 412]

List Comprehension

List synthesis is a trick that can use for loops and conditions when creating a new array. Using list synthesis will return a new array.

Its syntax is:

[expression for loop if condition]

For example:

[x ** 2 for x in range(10)] # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

# Equivalent to
nums = [1,2,3,4,5,6,7,8,9]
new_nums = []

for n in nums:
    new_nums.append(n ** 2)

Syntax to use if:

[x ** 2 for x in range(10) if x % 2 == 0] # [0, 4, 16, 36, 64]

Nesting of list consolidation:

list1 = [30, 50, 110, 40, 15, 75]
list2 = [10, 60, 20, 50]

[(n1, n2) for n1 in list1 for n2 in list2 if n1 + n2 > 100]
# [(50, 60), (110, 10), (110, 60), (110, 20), (110, 50), (75, 60), (75, 50)]

It's equivalent to running a for loop twice.

Posted by proctk on Thu, 21 Oct 2021 22:04:44 -0700