Language

Data Structures · Lesson 7 of 56

Lists

Source: 3-Data Structures/3.1-Lists.ipynb

Start here — no coding background needed

What you will learn

Store many values in one ordered list — add, remove, change items.

In simple words

A list keeps order: first item, second item… Use square brackets `[]`. Index 0 is the first item.

Think of it like this

A numbered shopping list you can edit — add at end, cross one off, change item 2.

Words to know:

  • list — Ordered collection in square brackets
  • append — Adds item at the end

Ways to store many values together — shopping lists, contacts, unique items.

Easy example — try this first

Easy example — run this first. Change values and press Run again.

Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference notes (from full bootcamp)

Optional — deeper detail for when you are ready

Introduction To Lists

  • Lists are ordered, mutable collections of items.
  • They can contain items of different data types.
Video Outline:
  1. Introduction to Lists
  2. Creating Lists
  3. Accessing List Elements
  4. Modifying List Elements
  5. List Methods
  6. Slicing Lists
  7. Iterating Over Lists
  8. List Comprehensions
  9. Nested Lists
  10. Practical Examples and Common Errors
Reference example
Python
Output
Expected (from notebook):
<class 'list'>

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
['Anshul', 'Jack', 'Jacob', 1, 2, 3, 4, 5]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
[1, 'Hello', 3.14, True]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
apple
cherry
gauva
gauva

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
['banana', 'cherry', 'kiwi', 'gauva']
['banana', 'cherry']

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
['apple', 'banana', 'cherry', 'kiwi', 'gauva']

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
['apple', 'watermelon', 'cherry', 'kiwi', 'gauva']

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
['apple', 'w', 'a', 't', 'e', 'r', 'm', 'e', 'l', 'o', 'n']

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
['apple', 'banana', 'cherry', 'kiwi', 'gauva', 'orange']

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
['apple', 'watermelon', 'banana', 'banana', 'cherry', 'kiwi', 'gauva', 'orange']

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
['apple', 'watermelon', 'banana', 'cherry', 'kiwi', 'gauva', 'orange']

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
orange
['apple', 'watermelon', 'banana', 'cherry', 'kiwi', 'gauva']

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
3

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
2

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
['apple', 'watermelon', 'banana', 'banana', 'cherry', 'kiwi', 'gauva']

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
['apple', 'banana', 'banana', 'cherry', 'gauva', 'kiwi', 'watermelon']

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
['watermelon', 'kiwi', 'gauva', 'cherry', 'banana', 'banana', 'apple']

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
[]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
[3, 4, 5]
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[1, 3, 5, 7, 9]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
[1, 4, 7, 10]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
[10, 8, 6, 4, 2]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
1
2
3
4
5
6
7
8
9
10

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

List Comprehension

Basics Syantax [expression for item in iterable]

with conditional logic [expression for item in iterable if condition]

Nested List Comprehension [expression for item1 in iterable1 for item2 in iterable2]

Reference example
Python
Output
Expected (from notebook):
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
[0, 2, 4, 6, 8]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
[0, 2, 4, 6, 8]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
[[1, 'a'], [1, 'b'], [1, 'c'], [1, 'd'], [2, 'a'], [2, 'b'], [2, 'c'], [2, 'd'], [3, 'a'], [3, 'b'], [3, 'c'], [3, 'd'], [4, 'a'], [4, 'b'], [4, 'c'], [4, 'd']]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
[5, 5, 6, 4, 13]

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Conclusion

List comprehensions are a powerful and concise way to create lists in Python. They are syntactically compact and can replace more verbose looping constructs. Understanding the syntax of list comprehensions will help you write cleaner and more efficient Python code.

Practice test — try yourself

Write code, press Check. Wrong answer shows the correct code to copy & run.

Create list colors = ["red","green","blue"]. Print the first color.

Python