The so-called list nesting means that a list contains other sub lists .
Application scenarios :
To store class one 、 Two 、 Names of students in three classes , And the names of the students in each class are in a list .
Such as , This is a large list, which contains three small lists , Each small list is a class , According to the basic syntax of the list, each data is separated by a comma :
name_list = [[' The small white ', ' indigo plant ', ' Xiaohong '], [' Wang Wu ', ' Li Si ', ' Zhang San '], ['Python', 'Java', 'Php']]
step :
First step : Find the sub list containing a certain data according to the subscript
The second step : From the list of words found , Then find the final desired data according to the subscript
Code experience :
name_list = [[' The small white ', ' indigo plant ', ' Xiaohong '], [' Wang Wu ', ' Li Si ', ' Zhang San '], ['Python', 'Java', 'Php']]
print(name_list) # result [[' The small white ', ' indigo plant ', ' Xiaohong '], [' Wang Wu ', ' Li Si ', ' Zhang San '], ['Python', 'Java', 'Php']]
# Data query when the list is nested
# Get the first word list
print(name_list[0]) # result :[' The small white ', ' indigo plant ', ' Xiaohong ']
# Get the second data in the first sub list ---- Think of the first word list as a list name, just write a subscript after it
print(name_list[0][1]) # result : indigo plant
Execution results as shown :
more Python For learning articles, you can go to Python Self study network Search for , There are not only basic knowledge articles, but also many examples for you to practice , Want to learn systematically Python The whole stack course It's OK .