Conteúdo do Curso
Estruturas de Dados em Python
Estruturas de Dados em Python
Lista aninhada
Os itens dentro de uma lista Python também podem ser listas, resultando em listas aninhadas, às vezes referidas como "listas dentro de listas". Aqui está um exemplo de uma lista aninhada:
numbers = [1, [1, 2, 3], 2, [3, 4, 5, [7, 9]]] print(numbers)
Using Variables for Nested Lists
This example illustrates a nested list in which the second and fourth elements are also lists, and the fourth element even contains another nested list within it.
You can also create nested lists by concatenating lists held in variables. This can be useful to make your code more readable. For instance:
europe_cities = ["Paris", "Berlin", "Rome"] asia_cities = ["Tokyo", "Seoul", "Bangkok"] america_cities = ["New York", "Los Angeles", "Chicago"] world_cities = [europe_cities, asia_cities, america_cities] print(world_cities)
Here, we first define three independent lists for cities in Europe, Asia, and the Americas. We then combine the lists into one, world_cities, which becomes a nested list.
The example shows that a nested list is a good way to organize related data, such as cities from different continents, in a natural, intuitive, and organized manner.
Tarefa
Construct a list called list_2
containing these elements:
[1, [2, 3], 4, [5, 6], 7]
Obrigado pelo seu feedback!
Lista aninhada
Os itens dentro de uma lista Python também podem ser listas, resultando em listas aninhadas, às vezes referidas como "listas dentro de listas". Aqui está um exemplo de uma lista aninhada:
numbers = [1, [1, 2, 3], 2, [3, 4, 5, [7, 9]]] print(numbers)
Using Variables for Nested Lists
This example illustrates a nested list in which the second and fourth elements are also lists, and the fourth element even contains another nested list within it.
You can also create nested lists by concatenating lists held in variables. This can be useful to make your code more readable. For instance:
europe_cities = ["Paris", "Berlin", "Rome"] asia_cities = ["Tokyo", "Seoul", "Bangkok"] america_cities = ["New York", "Los Angeles", "Chicago"] world_cities = [europe_cities, asia_cities, america_cities] print(world_cities)
Here, we first define three independent lists for cities in Europe, Asia, and the Americas. We then combine the lists into one, world_cities, which becomes a nested list.
The example shows that a nested list is a good way to organize related data, such as cities from different continents, in a natural, intuitive, and organized manner.
Tarefa
Construct a list called list_2
containing these elements:
[1, [2, 3], 4, [5, 6], 7]
Obrigado pelo seu feedback!
Lista aninhada
Os itens dentro de uma lista Python também podem ser listas, resultando em listas aninhadas, às vezes referidas como "listas dentro de listas". Aqui está um exemplo de uma lista aninhada:
numbers = [1, [1, 2, 3], 2, [3, 4, 5, [7, 9]]] print(numbers)
Using Variables for Nested Lists
This example illustrates a nested list in which the second and fourth elements are also lists, and the fourth element even contains another nested list within it.
You can also create nested lists by concatenating lists held in variables. This can be useful to make your code more readable. For instance:
europe_cities = ["Paris", "Berlin", "Rome"] asia_cities = ["Tokyo", "Seoul", "Bangkok"] america_cities = ["New York", "Los Angeles", "Chicago"] world_cities = [europe_cities, asia_cities, america_cities] print(world_cities)
Here, we first define three independent lists for cities in Europe, Asia, and the Americas. We then combine the lists into one, world_cities, which becomes a nested list.
The example shows that a nested list is a good way to organize related data, such as cities from different continents, in a natural, intuitive, and organized manner.
Tarefa
Construct a list called list_2
containing these elements:
[1, [2, 3], 4, [5, 6], 7]
Obrigado pelo seu feedback!
Os itens dentro de uma lista Python também podem ser listas, resultando em listas aninhadas, às vezes referidas como "listas dentro de listas". Aqui está um exemplo de uma lista aninhada:
numbers = [1, [1, 2, 3], 2, [3, 4, 5, [7, 9]]] print(numbers)
Using Variables for Nested Lists
This example illustrates a nested list in which the second and fourth elements are also lists, and the fourth element even contains another nested list within it.
You can also create nested lists by concatenating lists held in variables. This can be useful to make your code more readable. For instance:
europe_cities = ["Paris", "Berlin", "Rome"] asia_cities = ["Tokyo", "Seoul", "Bangkok"] america_cities = ["New York", "Los Angeles", "Chicago"] world_cities = [europe_cities, asia_cities, america_cities] print(world_cities)
Here, we first define three independent lists for cities in Europe, Asia, and the Americas. We then combine the lists into one, world_cities, which becomes a nested list.
The example shows that a nested list is a good way to organize related data, such as cities from different continents, in a natural, intuitive, and organized manner.
Tarefa
Construct a list called list_2
containing these elements:
[1, [2, 3], 4, [5, 6], 7]