Arvutiteaduse instituut
  1. Kursused
  2. 2025/26 sügis
  3. Programmeerimise alused (MTAT.03.236)
EN
Logi sisse

Programmeerimise alused 2025/26 sügis

  • Home Page
  • Introduction
  • Expressions
  • Conditional Execution
  • Functions
  • Iterations
  • Strings
  • Files
  • Lists
  • Graphics?
< previousTable of Contentsnext >

8.2 KEY CHARACTERISTICS OF PYTHON LISTS

ORDERING

As we mentioned before, Python keeps track of the order of elements in a list. The order allows you to access and manipulate list items based on their position. Each item in the list has a corresponding index, which is an integer value. Note, that the indexing does not start from 1 but stars from 0, meaning that the first element in the list is at index 0. To specify the index of the item we want to access, we add brackets [] and a corresponding index within the brackets at the end of the list’s name.

Here’s an example to illustrate in Thonny of accessing the second element Valge Mersu within the list of favorite songs.

MUTABILITY

Mutability refers to the ability to change the content of the list after it has been created. You can add, remove, or alter items in the list.


favorite_songs = ["Without Me", "Valge Mersu", "Tsirkus", "Baby"]

# Adding an item to the end of the list
favorite_songs.append("Bonkers") 
print(favorite_songs)  # The list now looks like this  [“Without Me”, “Valge Mersu”, “Tsirkus”, “Baby”, “Bonkers”]

# Removing a specified item from the list
favorite_songs.remove("Tsirkus")  
print(favorite_songs)  # Result [“Without Me”, “Valge Mersu”, “Baby”, “Bonkers”]

# We can change an item at a given index, this inserts the new item to the list instead of the previous
favorite_songs[1] = "Like That" 
print(favorite_songs)  # Result [“Without Me”, “Like That”, “Baby”, “Bonkers”]

DYNAMISM

Lists in Python are dynamic, meaning they can grow or shrink in size as needed. Hence, you can add new elements or remove existing ones.


# First we create an empty list
example_list = []

for i in range(5):
    example_list.append(i)  
print(dynamic_list)  # Output: [0, 1, 2, 3, 4]

while example_list: # this statement checks if the list is empty or not
    print(example_list.pop())  # Removes and prints to the console the returned item.

# Now if we print the example_list it will be empty again. 
print(example_list) # Outputs []

The default value of pop() is -1, which means it removes and returns the last item. However, you can specify other indices that you wish to remove too.

HETEROGENEITY

As mentioned before, lists can hold within them various different types of data, such as integers, floats, booleans, strings, characters and even other lists.


example_list = ["Hello", 57, False, 1.75, 't']

KEY THINGS TO REMEMBER

  • Lists can be altered. Use list[index] = value to modify items at specified indexes.
  • Lists are dynamic. They can grow and shrink in size as items are added or removed. Use append() to add items to the end of the list, pop() to remove and return an item from the specified index, -1 by default.
  • Lists can contain elements of different types, such as integers, floats, strings, characters other lists, etc.

SELF-CONTROL EXERCISES

< previousTable of Contentsnext >

  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused