LoslegenKostenlos loslegen

Using list methods to add data

You can use the .append() and .extend() methods to add elements to a list.

The .append() method increases the length of the list by one, so if you want to add only one element to the list, you can use this method.

x = [1, 2, 3]
x.append(4)
x

[1, 2, 3, 4]

The .extend() method increases the length of the list by the number of elements that are provided to the method, so if you want to add multiple elements to the list, you can use this method.

x = [1, 2, 3]
x.extend([4, 5])
x

[1, 2, 3, 4, 5]

Diese Übung ist Teil des Kurses

Introduction to Python for Finance

Kurs anzeigen

Anleitung zur Übung

  • Add a single element, 'Amazon.com', to the names.
  • Add the list more_elements which consists of two elements to names.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

# Append a name to the list names
names.____('Amazon.com')
print(names)

# Extend list names
more_elements = ['DowDuPont', 'Alphabet Inc']
names.____(____)
print(names)
Code bearbeiten und ausführen