List Comprehensions, Multidimensional Lists in python by CodingXpertz

Опубликовано: 28 Сентябрь 2024
на канале: Coding Xpertz
19
0

In this video we’ll cover List Comprehensions, Multidimensional Lists and you’ll have to solve
another problem.
List Comprehensions
You can construct lists in interesting ways using list comprehensions. You can do this by
performing an operation on each item in the list.
CODE
import math
Create a list of even values
even_list = [i*2 for i in range(10)]


for k in even_list:
print(k, end=", ")
print()


List of lists containing values to the power of
2, 3, 4
num_list = [1,2,3,4,5]


list_of_values = [[math.pow(m, 2), math.pow(m, 3), math.pow(m, 4)]
for m in num_list]


for k in list_of_values:
print(k)
print()


Create a 10 x 10 list
multi_d_list = [[0] * 10 for i in range(10)]


Change a value in the multidimensional list
multi_d_list[0][1] = 10


Get the 2nd item in the 1st list
It may help to think of it as the 2nd item in the 1st row
print(multi_d_list[0][1])


Get the 2nd item in the 2nd list
print(multi_d_list[1][1])
Multidimensional Lists
Multidimensional list are tables of data that spans across rows and columns. Here I’ll show
how indexes work with a multidimensional list