Skip to content

Joseph-T-Gordon

A complete log of all recent projects and skills

Menu
  • About me
  • Posts/Projects
  • Resume/Certifications
Menu

Post #41 (Python Programming) – Lists in lists, the power of matrices

Posted on March 30, 2023June 26, 2023 by admin

In python believe it or not you actually have the power to put lists within lists. How would one do that? You can use the following code

list = [1, 2, 3, 4, 5]

for i in range(8):

list_2 = [1, 2, 3, 4, 5]

list.append(list_2)

print(list)

The above code creates a list within a list, there’s an even easier way to quickly create a list at runtime. The first element is the value being stored in the list and the rest is the list you’re creating.

list = [0.0 for x in range(8)]

print(list)

You can even nest lists this way as well, the following code creates a nested list, with 24 rows and 31 columns and assigns them all the value of “EMPTY” then prints the value stored in list index 24,31

Hours_in_Month = [[“EMPTY” for i in range(31)] for x in range(24)]

print(Hours_in_Month)

print(Hours_in_Month[23][30])

You can even set values to specific indexes in your nested lists.

Hours_in_Month = [[“EMPTY” for i in range(31)] for x in range(24)]

print(Hours_in_Month)

print(Hours_in_Month[23][30])

Hours_in_Month[0][0] = “Occupied”

print(Hours_in_Month[0][0])

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

© 2025 Joseph-T-Gordon | Powered by Minimalist Blog WordPress Theme