Python Lists: Nested Structures and List Comprehensions Explained
This tutorial walks through Python list basics, showing how to define one‑, two‑ and three‑dimensional lists, access elements via indexing, and use list comprehensions to generate sequences, filter odd numbers, and compute squares, with complete code examples and their outputs.
The note introduces Python list fundamentals, starting with one‑dimensional, two‑dimensional and three‑dimensional list definitions and demonstrates how to access elements using index notation.
list1 = [12,34,54,65,7] # one‑dimensional list
print(list1[3]) # → 65
list2 = [32,4,545,6,["hello",434,6565,78],44343] # two‑dimensional list
print(list2[4][2]) # → 6565
list3 = [44,45,6,7,[43,53,57,[63,74,83,25,64]],56,97] # three‑dimensional list
print(list3[4][3][3]) # → [63,74,83,25,64]Next, the author explains list comprehensions by first showing the explicit creation of a list of integers 1‑10 and then converting the same range with list(range(1,11)). A loop‑based method for generating squares 1‑5 is presented, followed by the equivalent comprehension syntax.
list4 = [i**2 for i in range(1,6)]
print(list4) # → [1,4,9,16,25]Additional comprehensions illustrate filtering: extracting odd numbers from 1‑10 and further selecting numbers that are both odd and divisible by 3.
list5 = [i for i in range(1,11) if i % 2 == 1]
print(list5) # → [1,3,5,7,9]
list6 = [i for i in range(1,11) if i % 2 == 1 and i % 3 == 0]
print(list6) # → [3,9]All example outputs are shown, confirming the correct behavior of nested indexing and comprehension filters.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
