-2

As a novice I am trying to understand a particular part of len() .

friends = ["Alice", "Bob", "Carl", "John"]
print(len(friend))                         # answer = 4 (number of items in list)
friend.extend([1, 2, 3])  
print(len(friend))                         # answer = 7 (where does the "7" come from and what is it 
                                           # telling me? 
azro
  • 53,056
  • 7
  • 34
  • 70
James
  • 1
  • 1
  • 2
    list becomes `['Alice', 'Bob', 'Carl', 'John', 1, 2, 3]` so there is 7 elements – azro May 30 '21 at 12:16
  • Is your question : what does list.extend do ? do `print(friend)` between each, you'll understand – azro May 30 '21 at 12:16
  • 1
    just check what `len(list)` and `list.extend` function do, and the effect of each operation on the orignal list – sahasrara62 May 30 '21 at 12:16
  • 1
    Does this answer your question? [What is the difference between Python's list methods append and extend?](https://stackoverflow.com/questions/252703/what-is-the-difference-between-pythons-list-methods-append-and-extend) – Ganesh Tata May 30 '21 at 12:18

1 Answers1

0

When you call the extend method for a list with another list as an argument, you're basically combining both lists so all those elements, 1, 2, 3 are also stored in the list friends (by the way you have a typo, it should be friends.extend() not friend.extend()). So 4 elements + 3 elements gives you 7. It returns the length or how much elements the list has, which is 7 in this case since you added 3 more elements.