Different data types or objects can be listed in a so-called list (also a sequential data type):

person = ["Eick", "Wolfhard", 50, 1.84, ["Léon", "Stella", "Olivia"]]

List in Python

person can be printed easely:

print(person)     # output: ['Eick', 'Wolfhard', 50, 1.84, ['Léon', 'Stella', 'Olivia']]


List functions

Python offers some functions that are very helpful for processing lists:

len() returns the length of the list.

 n = len(person)     # output 5

With append() another object can be appended to the end of the list.

person.append("male")    # person = ['Eick', 'Wolfhard', 50, 1.84, ['Léon', 'Stella', 'Olivia'], 'male']


pop()  removes the last object from the list and returns it.

person.pop()    # 'male'
# ['Eick', 'Wolfhard', 50, 1.84, ['Léon', 'Stella', 'Olivia']]
If pop() has a parameter, the object in the list is deleted and output at the position specified by the parameter.

person.pop(3)   # 1.84      
# person = ['Eick', 'Wolfhard', 50, ['Léon', 'Stella', 'Olivia']]


insert() inserts an object into the list at the position specified in the parameter.

person.insert(2,"Teacher Plus") 
# person = ['Eick', 'Wolfhard', 'Teacher Plus', 50, ['Léon', 'Stella', 'Olivia']]

remove() removes a specific object from the list. The position of the object is irrelevant.

person.remove("Wolfhard")
# person = ['Eick', 'Teacher Plus', 50, ['Léon', 'Stella', 'Olivia']]

Last modified: Sunday, 27 October 2019, 12:29 PM