loop
Lists and list processing

Module 3 · Lists and list processing · Lesson 3 of 7

Reading and changing content

Indexing isn't just for reading. Because lists are mutable — changeable — you can also assign to a position and replace whatever's there. nums[1] = 99 swaps the second element for 99, in place, leaving everything else exactly as it was.

nums[1] = 99
010
120
230
340

Only slot [1] changes — the list stays the same length, the same object.

That's a real difference from the values you've met so far. A number or a string can't be changed — you can only make a new one. A list, though, is a container you can keep rearranging without giving it a new name.

Run it yourselfruns in your browser · Python
main.py

Two everyday helpers round this out: len(nums) gives the number of items, and value in nums answers a yes/no question — is that value anywhere in the list? — with a boolean. Both come up constantly. Next: making the list longer.