Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

extend python

# Extend Method on Lists 
# The extend() method adds a list variable to another list 
# specified as an argument when you run the list's method.

# We have a list of random names here:
random_names = ["Isabella", "Laura", "Robert", "Andrew", "Bob", "Angela"]

# Print the current list to the terminal so that we can view the before and
# after afterwards.

print(random_names)

"""
The extend method is a method because a list is an object/class created 
behind the scenes. To utilize it, you must type the list variable name,
then a dot, followed by the word "extend" with parentheses afterwards.
Then inside those parentheses, make a tiny list. Let's say there is Bob's
friend, Stanley. And Stanley's other friend is Tristan. Let's add those
name strings to the list! So the syntax is:
"""

random_names.extend(["Stanley", "Tristan"])

"""Under the hood, how this is working is basically the list we defined as
an argument down below will be added to the list we put before the dot like
this:

random_names = random_names + ["Stanley", "Tristan"]
"""

# Lastly, we print the list to see the new changes

print(random_names)

 
PREVIOUS NEXT
Tagged: #extend #python
ADD COMMENT
Topic
Name
2+4 =