Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Nested Lists in Python | Other Data Types in Python
Introduction to Python (dev copy)

bookNested Lists in Python

As mentioned in the opening chapter, lists in Python can store various data types, even including other lists. What's unique about Python, compared to many other programming languages, is that these nested lists can differ in length.

Let's dive into an example. Imagine we have data about countries and their respective areas. Instead of jamming all that information into a single list, it would be more organized to have a distinct list for each country.

123
# Two-dimensional list countries_2d = [["USA", 9629091], ["Canada", 9984670], ["Germany", 357114]] print(countries_2d)
copy

As illustrated, every country has its dedicated list nested within the primary list. Now, you might wonder, how do we navigate through such a structure? If we're dealing with a two-dimensional list, its elements are also lists. And, we can access the inner list's items using indexing.

Consider a two-dimensional list named countries_2d that contains 3 main elements (which are lists). Each of these lists has 2 items.

So, countries_2d[1] fetches the second list in the main list (keep in mind, Python indexing begins at 0). Moreover, countries_2d[1][0] retrieves the first item within that second list.

Check out the example below for clarity.

123456
# Two-dimensional list countries_2d = [['USA', 9629091], ['Canada', 9984670], ['Germany', 357114]] # Pull elements print(countries_2d[1]) print(countries_2d[1][0])
copy

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 1.64

bookNested Lists in Python

Scorri per mostrare il menu

As mentioned in the opening chapter, lists in Python can store various data types, even including other lists. What's unique about Python, compared to many other programming languages, is that these nested lists can differ in length.

Let's dive into an example. Imagine we have data about countries and their respective areas. Instead of jamming all that information into a single list, it would be more organized to have a distinct list for each country.

123
# Two-dimensional list countries_2d = [["USA", 9629091], ["Canada", 9984670], ["Germany", 357114]] print(countries_2d)
copy

As illustrated, every country has its dedicated list nested within the primary list. Now, you might wonder, how do we navigate through such a structure? If we're dealing with a two-dimensional list, its elements are also lists. And, we can access the inner list's items using indexing.

Consider a two-dimensional list named countries_2d that contains 3 main elements (which are lists). Each of these lists has 2 items.

So, countries_2d[1] fetches the second list in the main list (keep in mind, Python indexing begins at 0). Moreover, countries_2d[1][0] retrieves the first item within that second list.

Check out the example below for clarity.

123456
# Two-dimensional list countries_2d = [['USA', 9629091], ['Canada', 9984670], ['Germany', 357114]] # Pull elements print(countries_2d[1]) print(countries_2d[1][0])
copy

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 4
some-alt