Python basic core classic tutorial (021) - data structure public method

Copyright notice

  • The author of this article: Gu Ge's younger brother
  • Author's blog address:


The so-called common method of data structure refers to the method that can be used by data structure such as list, tuple, dictionary, string, etc. Here, we summarize the common methods and operations of data structure in Python.

Built in functions

Here is a brief introduction to the common built-in functions included in Python

function describe remarks
len(item) Count the number of elements in the container Also known as the number of data
Del or del(item) Remove variables from container Two ways to pay attention to del
max(item) Find maximum in container If it's a dictionary, only for key comparison
min(item) Find minimum in container If it's a dictionary, only for key comparison


//Original author: Gu Ge's younger brother
//Blog address:
//Example description: data structure common method

s1 = 'abcde'
result = len(s1)

s2 = 'abcde'
del s2
# name 's2' is not defined
# print(s2)

l1 = [100, 200, 300, 400]
del (l1[2])

s3 = 'abcde'
result = max(s3)
result = min(s3)

l2 = [100, 200, 300, 400]
result = max(l2)
result = min(l2)


operator Example result describe Supported data types
+ [9,5]+[2,7] [9, 5, 2, 7] merge String, list, tuple
* "tom"*4 tomtomtomtom repeat String, list, tuple
in 9 in (9, 5, 2,7) True Element exists String, list, tuple, dictionary
not in 9 not in (4, 5, 6) True Element does not exist String, list, tuple, dictionary
>Or > = or = = or < or<= (4, 5, 6) < (5, 5, 6) True Element comparison String, list, tuple

be careful:

  • in the operation of dictionary, the key of dictionary is judged
  • When not in operates a dictionary, it judges the key of the dictionary


Original author: Gu Ge's younger brother
 Blog address:
 Example description: data structure common method
result = [9, 5] + [2, 7]

result = "tom" * 4

result = (4, 5, 6) < (5, 5, 6)

Mutual transformation of data structure

  • tuple() converts other data structures into tuples
  • list() converts other data structures to lists
  • set() converts other data structures to sets


//Original author: Gu Ge's younger brother
//Blog address:
//Example description: mutual transformation of data structure
l1 = [100, 200, 300, 400, 500]
result = tuple(l1)
s1 = {100, 200, 300, 400, 500}
result = tuple(s1)

t1 = ('a', 'b', 'c', 'd', 'e')
result = list(t1)
s2 = {100, 200, 300, 400, 500}
result = list(s2)

l2 = [100, 200, 300, 400, 500, 100, 200]
result = set(l2)
t2 = ('a', 'b', 'c', 'd', 'e')
result = set(t2)


You can remove duplicate elements from a list by rotating the list into a collection.


Slicing refers to the operation of intercepting part of the original data. String, list and tuple all support slicing operation.

The syntax is as follows:

Original data [start position subscript: end position subscript: step size]

The main points are as follows:

  • 1. The section specified by the slice belongs to the left closed and right open type, i.e. including the beginning and not including the end.
  • 2. If you slice from the beginning, the start position subscript can be omitted, but the colon after it cannot be omitted
  • 3. If the slice ends at the end, the ending position subscript can be omitted, but the colons before and after it cannot be omitted
  • 4. The default step size is 1; if the slice is continuous, the colon and subsequent step size values can be omitted

Index order:

Python supports not only sequential index, but also reverse index, as shown below:

Sequential index is easy to understand and often used; so, what is reverse index? The so-called reverse index is to calculate the index from right to left. The rightmost index value is - 1, decreasing from right to left.

in other words:
The order index of characters in python is 0 1 2 3 4 5
The reverse index of python's characters is - 1 - 2 - 3 - 4 - 5 - 6

It can be seen from this that:
The index of p can be expressed as either 0 or - 6
The index of y can be expressed in either 1 or - 5
Other characters are similar; we will not repeat here.


//Original author: Gu Ge's younger brother
//Blog address:
//Example description: slice operation of data structure
# Definition string
number = "0123456789"

# Intercepts a string from subscript 1 to subscript 3
result = number[1:4]

# Intercept the string from the beginning to the end of subscript 4
result = number[4:]

# Intercept string from start to subscript 6
result = number[:7]

# Truncate full string
result = number[:]

# Intercept the string every other character from the start position
result = number[::2]

# From the position of subscript 1, every one character interval is intercepted
result = number[1::2]

# Intercept last string
result = number[-1]

# Intercepts the string from the subscript 2 position to the - 4 position
result = number[2:-4]

# Intercepts the last two characters of a string
result = number[-2:]

# Reverse string order
result = number[::-1]

Tags: Python

Posted on Sun, 14 Jun 2020 22:19:54 -0400 by Serpent7