swcarpentry / python-novice-gapminder

Plotting and Programming in Python
http://swcarpentry.github.io/python-novice-gapminder/
Other
163 stars 429 forks source link

List Introduction: Extract `extend` functionality from section about `append` #680

Open saiboxx opened 5 months ago

saiboxx commented 5 months ago

How could the content be improved?

Hi Carpentry Team,

When working through the "Lists" episode I noticed potential for streamlining and isolating some proposed concepts. The episode contains a section about using append to add items to a list. The last bullet point introduces the function extend, which is an important and often used utility. However, I see some problems with the current state:

Proposal:

  1. Extract the extend functionality into a separate section with the context of merging lists.
  2. Make the example more intuitive.
  3. Use the opportunity to introduce the + operator for list merging.
  4. Move the concept of nested lists to the section about different types in lists.

For 1. and 2.:

## Multiple lists can be combined.
- `extend` is a list *method* like `append` but allows you to combine two lists.  For example:
primes = [2, 3, 5, 7]
more_primes = [11, 13, 17, 19]
print('primes is currently:', primes)
primes.extend(more_primes)
print('primes has now become:', primes)
primes is currently: [2, 3, 5, 7]
primes has now become: [2, 3, 5, 7, 11, 13, 17, 19]

For 3.:

- We can also combine lists using the `+` operator, but instead of altering an existing list, it creates a new one.
primes = [2, 3, 5, 7]
more_primes = [11, 13, 17, 19]
combined_primes = primes + more_primes
print('combined_primes is now:', combined_primes)
combined_primes is now: [2, 3, 5, 7, 11, 13, 17, 19]

For 4. add to the types section:

- This implies that lists can store other lists, allowing arbitrary nested lists.
more_goals = [4, 'Explore lists.', [5, 'Understand lists within lists.', [6, 'Master Python lists.']]]

The changes are also contained in a PR #679 I have made previously. I apologize for the inconvenience caused, as I didn't open an issue first. I agree that the proposed changes should be talked about with the community.

Cheers, Tobias

Which part of the content does your suggestion apply to?

http://swcarpentry.github.io/python-novice-gapminder/11-lists.html#appending-items-to-a-list-lengthens-it.