Working with 2D Lists in Python: A Comprehensive Guide

undefined
 
I
n
t
r
o
d
u
c
t
i
o
n
 
t
o
C
o
m
p
u
t
e
r
 
S
c
i
e
n
c
e
 
Programming with Python
undefined
 
2
D
 
A
r
r
a
y
s
 
/
 
L
i
s
t
 
o
f
 
L
i
s
t
s
 
 
L
i
s
t
 
o
f
 
L
i
s
t
s
 
A list in Python can contain multiple lists
A list of lists in other programming languages is known as 2D Array/List
It is very useful to store Matrix-like data such as graphs, charts, etc
2 Dimensional Array
 
L
i
s
t
 
o
f
 
L
i
s
t
s
 
We can define list of lists as follows
 
    Or more human readable
 
a = [[
1
, 
2
, 
3
]
, 
[
4
, 
5
, 
6
]]
a = [
    [
1
, 
2
, 
3
]
,
    
[
4
, 
5
, 
6
]
    ]
 
Row 0
Row 1
 
column
1
 
column
2
 
column
3
 
A
 
2
-
D
i
m
e
n
s
i
o
n
a
l
 
A
r
r
a
y
 
L
e
n
g
t
h
 
o
f
 
R
o
w
s
 
a
n
d
 
C
o
l
u
m
n
s
 
It is possible to get how many rows and columns of the Matrix or list of lists using
the len function
 
       
length of a: 2
       
(number of rows / lists)
       
           3 columns in row 0
a = [
    [
1
, 
2
, 
3
]
,
    
[
4
, 
5
, 
6
]
    ]
print
(
"length of a: "
, 
len
(a))
print
(
len
(a[
0
])
, 
" columns in row 0"
)
 
T
h
e
 
a
p
p
e
n
d
(
)
 
f
u
n
c
t
i
o
n
 
w
i
t
h
 
2
D
 
L
i
s
t
s
 
Just like lists, we can use the append function to insert data to the list
We can add to an existing list. In other words, add a column to an existing row or
add a new row
Remember, lists are mutable so list of lists are also mutable
 
    
      add 100 to row 0 – this will increate the length
    
      of row 0
    
                 This will add a new row to the list
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
T
h
i
s
 
w
i
l
l
 
a
d
d
 
a
 
n
u
m
b
e
r
 
w
i
t
h
o
u
t
 
a
 
l
i
s
t
 
-
 
d
o
 
n
o
t
 
d
o
 
i
t
                  Instead, add a single value in a row itself / list of 1 value
a[
0
].append(
100
)
a.append([
120
,
200
,
320
])
a.append(
100
)
a.append([
100
])
 
D
e
f
i
n
i
t
e
 
L
o
o
p
s
 
w
i
t
h
 
2
D
 
L
i
s
t
s
 
/
 
L
i
s
t
 
o
f
 
L
i
s
t
s
 
It is possible to loop through 2D array using the classic way that works with all
programming languages. This is good because it will give you an insight of how to
deal with 2 Dimensional Lists/Arrays
Nested for loops
 First loop will go through rows
Second loop will go through each column of the row. Or, each element in a specific row
 
D
e
f
i
n
i
t
e
 
L
o
o
p
s
 
w
i
t
h
 
2
D
 
L
i
s
t
s
 
/
 
L
i
s
t
 
o
f
 
L
i
s
t
s
a = [
    [
1
, 
2
, 
3
]
,
    
[
4
, 
5
, 
6
]
,
    
[
7
, 
8
, 
9
, 
0
]
    ]
for 
row 
in 
range
(
0
, 
len
(a)):
    
for 
col 
in 
range
(
0
, 
len
(a[row])):
        
print
(a[row][col]
, 
end
=
"
\t
"
)
    
print
()
 
D
e
f
i
n
i
t
e
 
L
o
o
p
s
 
w
i
t
h
 
2
D
 
L
i
s
t
s
 
/
 
L
i
s
t
 
o
f
 
L
i
s
t
s
a = [
    [
1
, 
2
, 
3
]
,
    
[
4
, 
5
, 
6
]
,
    
[
7
, 
8
, 
9
, 
0
]
    ]
print
(
"element of a[2][3]"
)
 
?
 
D
e
f
i
n
i
t
e
 
L
o
o
p
s
 
w
i
t
h
 
2
D
 
L
i
s
t
s
 
/
 
L
i
s
t
 
o
f
 
L
i
s
t
s
a = [
    [
1
, 
2
, 
3
]
,
    
[
4
, 
5
, 
6
]
,
    
[
7
, 
8
, 
9
, 
0
]
    ]
print
(
"element of a[2][3]"
)
 
D
e
f
i
n
i
t
e
 
L
o
o
p
s
 
w
i
t
h
 
2
D
 
L
i
s
t
s
 
/
 
L
i
s
t
 
o
f
 
L
i
s
t
s
a = [
    [
1
, 
2
, 
3
]
,
    
[
4
, 
5
, 
6
]
,
    
[
7
, 
8
, 
9
, 
0
]
    ]
for 
row 
in 
range
(
0
, 
len
(a)):
    
for 
col 
in 
range
(
0
, 
len
(a[row])):
        
print
(a[row][col]
, 
end
=
"
\t
"
)
    
print
()
 
Debugging table consists of 10 rows
(excluding header)
Slide Note
Embed
Share

Explore the world of 2D lists in Python programming, understanding how to manipulate and iterate through list of lists effectively. Learn how to create, access, append, and loop through 2D arrays to manage matrix-like data structures efficiently.

  • Python Programming
  • 2D Lists
  • List Manipulation
  • Iteration
  • Data Structures

Uploaded on Sep 24, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Introduction to Computer Science Programming with Python

  2. 2D Arrays / List of Lists

  3. List of Lists A list in Python can contain multiple lists A list of lists in other programming languages is known as 2D Array/List It is very useful to store Matrix-like data such as graphs, charts, etc 2 Dimensional Array

  4. List of Lists We can define list of lists as follows a = [[1, 2, 3], [4, 5, 6]] Or more human readable a = [ [1, 2, 3], [4, 5, 6] ] Row 0 Row 1 column 1 column 2 column 3

  5. A 2-Dimensional Array

  6. Length of Rows and Columns It is possible to get how many rows and columns of the Matrix or list of lists using the len function a = [ [1, 2, 3], [4, 5, 6] ] length of a: 2 (number of rows / lists) print("length of a: ", len(a)) 3 columns in row 0 print(len(a[0]), " columns in row 0")

  7. The append() function with 2D Lists Just like lists, we can use the append function to insert data to the list We can add to an existing list. In other words, add a column to an existing row or add a new row Remember, lists are mutable so list of lists are also mutable a[0].append(100) add 100 to row 0 this will increate the length of row 0 This will add a new row to the list a.append([120,200,320]) This will add a number without a list - do not do it Instead, add a single value in a row itself / list of 1 value a.append([100]) a.append(100)

  8. Definite Loops with 2D Lists / List of Lists It is possible to loop through 2D array using the classic way that works with all programming languages. This is good because it will give you an insight of how to deal with 2 Dimensional Lists/Arrays Nested for loops First loop will go through rows Second loop will go through each column of the row. Or, each element in a specific row

  9. Definite Loops with 2D Lists / List of Lists a = [ [1, 2, 3], [4, 5, 6], [7, 8, 9, 0] ] for row in range(0, len(a)): for col in range(0, len(a[row])): print(a[row][col], end="\t") print()

  10. Definite Loops with 2D Lists / List of Lists a = [ [1, 2, 3], [4, 5, 6], [7, 8, 9, 0] ] ? print("element of a[2][3]")

  11. Definite Loops with 2D Lists / List of Lists a = [ [1, 2, 3], [4, 5, 6], [7, 8, 9, 0] ] print("element of a[2][3]")

  12. Definite Loops with 2D Lists / List of Lists a = [ [1, 2, 3], [4, 5, 6], [7, 8, 9, 0] ] Debugging table consists of 10 rows (excluding header) Row Col a[row][col] / value for row in range(0, len(a)): for col in range(0, len(a[row])): print(a[row][col], end="\t") print()

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#