Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lists (3/3) | Other data types
Learn Python from Scratch
course content

Contenido del Curso

Learn Python from Scratch

Learn Python from Scratch

1. The basics
2. Arithmetic operations
3. Common data types
4. Conditional statements
5. Other data types
6. Loops
7. Functions

Lists (3/3)

As we mentioned before, you can store different data types inside a list. It means you can store lists inside lists - it means you can create multidimensional arrays. Let's experiment with two-dimensional arrays.

Remember our example with countries and areas? In that list each characteristic (name and area) is equivalent in terms of location - I think you agree that it would be much better if we could get both characteristics by only one index.

To create a two-dimensional list just put a list inside another. If you will write list[1] you will get the second element which is a list. At the same time if you write both indexes list[1][1] - you will get the second element of the second list in the list. For example, let's modify our previous example:

12
# create two-dimensional list countries_mod = [['USA', 9629091], ['Canada', 9984670], ['Germany', 357114], ['Brazil', 8515767], ['India', 3166391]]
copy

Now we can easily get both countries' names and areas by only one index. For example, let's extract information for Brazil and get Germany's area.

12345678
# create two-dimensional list countries_mod = [['USA', 9629091], ['Canada', 9984670], ['Germany', 357114], ['Brazil', 8515767], ['India', 3166391]] # list about Brazil print(countries_mod[3]) # area of Germany print(countries_mod[2][1])
copy

Tarea

  1. Recreate list (name as people) with people names and ages into two-dimensional.
  2. Print the information for second person
  3. Print age of fifth person.
NameAge
Alex23
Noah34
Peter29
John41
Michelle35

Tarea

  1. Recreate list (name as people) with people names and ages into two-dimensional.
  2. Print the information for second person
  3. Print age of fifth person.
NameAge
Alex23
Noah34
Peter29
John41
Michelle35

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 5. Capítulo 3
toggle bottom row

Lists (3/3)

As we mentioned before, you can store different data types inside a list. It means you can store lists inside lists - it means you can create multidimensional arrays. Let's experiment with two-dimensional arrays.

Remember our example with countries and areas? In that list each characteristic (name and area) is equivalent in terms of location - I think you agree that it would be much better if we could get both characteristics by only one index.

To create a two-dimensional list just put a list inside another. If you will write list[1] you will get the second element which is a list. At the same time if you write both indexes list[1][1] - you will get the second element of the second list in the list. For example, let's modify our previous example:

12
# create two-dimensional list countries_mod = [['USA', 9629091], ['Canada', 9984670], ['Germany', 357114], ['Brazil', 8515767], ['India', 3166391]]
copy

Now we can easily get both countries' names and areas by only one index. For example, let's extract information for Brazil and get Germany's area.

12345678
# create two-dimensional list countries_mod = [['USA', 9629091], ['Canada', 9984670], ['Germany', 357114], ['Brazil', 8515767], ['India', 3166391]] # list about Brazil print(countries_mod[3]) # area of Germany print(countries_mod[2][1])
copy

Tarea

  1. Recreate list (name as people) with people names and ages into two-dimensional.
  2. Print the information for second person
  3. Print age of fifth person.
NameAge
Alex23
Noah34
Peter29
John41
Michelle35

Tarea

  1. Recreate list (name as people) with people names and ages into two-dimensional.
  2. Print the information for second person
  3. Print age of fifth person.
NameAge
Alex23
Noah34
Peter29
John41
Michelle35

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 5. Capítulo 3
toggle bottom row

Lists (3/3)

As we mentioned before, you can store different data types inside a list. It means you can store lists inside lists - it means you can create multidimensional arrays. Let's experiment with two-dimensional arrays.

Remember our example with countries and areas? In that list each characteristic (name and area) is equivalent in terms of location - I think you agree that it would be much better if we could get both characteristics by only one index.

To create a two-dimensional list just put a list inside another. If you will write list[1] you will get the second element which is a list. At the same time if you write both indexes list[1][1] - you will get the second element of the second list in the list. For example, let's modify our previous example:

12
# create two-dimensional list countries_mod = [['USA', 9629091], ['Canada', 9984670], ['Germany', 357114], ['Brazil', 8515767], ['India', 3166391]]
copy

Now we can easily get both countries' names and areas by only one index. For example, let's extract information for Brazil and get Germany's area.

12345678
# create two-dimensional list countries_mod = [['USA', 9629091], ['Canada', 9984670], ['Germany', 357114], ['Brazil', 8515767], ['India', 3166391]] # list about Brazil print(countries_mod[3]) # area of Germany print(countries_mod[2][1])
copy

Tarea

  1. Recreate list (name as people) with people names and ages into two-dimensional.
  2. Print the information for second person
  3. Print age of fifth person.
NameAge
Alex23
Noah34
Peter29
John41
Michelle35

Tarea

  1. Recreate list (name as people) with people names and ages into two-dimensional.
  2. Print the information for second person
  3. Print age of fifth person.
NameAge
Alex23
Noah34
Peter29
John41
Michelle35

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

As we mentioned before, you can store different data types inside a list. It means you can store lists inside lists - it means you can create multidimensional arrays. Let's experiment with two-dimensional arrays.

Remember our example with countries and areas? In that list each characteristic (name and area) is equivalent in terms of location - I think you agree that it would be much better if we could get both characteristics by only one index.

To create a two-dimensional list just put a list inside another. If you will write list[1] you will get the second element which is a list. At the same time if you write both indexes list[1][1] - you will get the second element of the second list in the list. For example, let's modify our previous example:

12
# create two-dimensional list countries_mod = [['USA', 9629091], ['Canada', 9984670], ['Germany', 357114], ['Brazil', 8515767], ['India', 3166391]]
copy

Now we can easily get both countries' names and areas by only one index. For example, let's extract information for Brazil and get Germany's area.

12345678
# create two-dimensional list countries_mod = [['USA', 9629091], ['Canada', 9984670], ['Germany', 357114], ['Brazil', 8515767], ['India', 3166391]] # list about Brazil print(countries_mod[3]) # area of Germany print(countries_mod[2][1])
copy

Tarea

  1. Recreate list (name as people) with people names and ages into two-dimensional.
  2. Print the information for second person
  3. Print age of fifth person.
NameAge
Alex23
Noah34
Peter29
John41
Michelle35

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 5. Capítulo 3
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt