Topic Overview and Acclimation

Welcome back, young programmer! Today, we're stepping into a new Python realm: Nested Lists. A nested list is essentially a list containing other lists crucial for managing intricate data, like a grocery list, where each item is a category containing a list of items. Our goal? To make you comfortable with nested lists and show you some advanced operations they can perform. So, buckle up!

Understanding Nested Lists

Nested lists are lists within lists, key to managing complex datasets. Here's an example of a simple nested list:

Three lists reside within a larger list. But how do we access and modify the elements of these sublists? Let's delve into this.

Nested List Indexing

We can access items in nested lists with indices, similar to the flat lists we covered in the previous unit. In Python, indexing runs from 0 (from left to right) or -1 (from right to left). To access elements of sublists, we use two indices. Let's illustrate this:

Here, [0][0] fetched 1 from the first sublist, and [1][2] fetched "cherry" from the second sublist. [-1][2] references the third element in the last list, which is the same element as [1][2].

Modifying Nested Lists

Modifying the elements of a list or sublist is as easy as knowing the index of the element. Let's change 'apple' to 'kiwi' in the second sublist:

We can add new elements using append(), which adds an item at the end of a list. We can also append() a whole new list to the nested list the same way.

Advanced Operations on Lists

Let's tackle advanced operations with Python's list of methods that tend to be very useful when operating with lists.

  • append(): Adds an item to the end of the list.
  • extend(): Appends multiple items to the list.
  • insert(index, element): Inserts an item at a specific position in the list.
  • remove(element): Removes the first occurrence of the specified element from the list.
  • pop(index): Removes and returns the item at the specified index. By default, removes the last item.
  • clear(): Removes all items from the list.
  • index(element): Returns the index of the first occurrence of the specified element.
  • count(element): Returns the number of occurrences of the specified element.
  • sort(): Sorts the items in ascending order. Use sort(reverse=True) for descending order.
  • reverse(): Reverses the order of items in the list.
  • copy(): Returns a shallow copy of the list.

Just a friendly note: you don't have to remember all these methods right now, you can always refer to language documentation or me (Cosmo) in case you need help to remember them! In the meantime, here is an example of how all these methods work:

It's worth mentioning that some of these methods (like pop()) work on nested lists, too! However, some of them (like sort()) do not work on nested lists and can only be applied to a one-dimensional list.

Takeaways and Next Steps

Great job! We’ve navigated through the realm of nested lists, learning not just about what they are and how they work but also how methods like append(), count(), and others apply to both flat and nested lists. Are you ready for some hands-on exercises? They’re coming up next! These lessons will reinforce your understanding and help you become comfortable with these concepts. See you soon!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal