2. Module 2 Lists - collections of data
Why do we need lists?
var1 = int(input())
var2 = int(input())
var3 = int(input())
var4 = int(input())
var5 = int(input())
var6 = int(input())
:
:
declare such
multi-value variables
numbers = [10, 5, 7, 2, 1]
List is a collection of elements, but each element is a scalar;
the elements in a list are always numbered starting from zero.
3. Module 2 Lists - collections of data
Indexing lists
numbers = [10, 5, 7, 2, 1]
print("Original list content:", numbers) # Printing original list content.
numbers[0] = 111
print("nPrevious list content:", numbers) # Printing previous list content.
numbers[1] = numbers[4] # Copying value of the fifth element to the second.
print("New list content:", numbers) # Printing current list content.
Original list content: [10, 5, 7, 2, 1]
Previous list content: [111, 5, 7, 2, 1]
New list content: [111, 1, 7, 2, 1]
4. Module 2 Lists - collections of data
Accessing list content
numbers = [10, 5, 7, 2, 1]
# Accessing the list's first element.
print(numbers[0])
numbers = [10, 5, 7, 2, 1]
# Printing the list's length.
print("List length:", len(numbers))
Each of the list's elements may be accessed separately.
List length: 5
5. Module 2 Lists - collections of data
Removing elements from a list
numbers = [10, 5, 7, 2, 1]
print("Original list content:", numbers) # Printing original list content.
numbers[0] = 111
print("nPrevious list content:", numbers) # Printing previous list content.
numbers[1] = numbers[4] # Copying value of the fifth element to the second.
print("Previous list content:", numbers) # Printing previous list content.
print("nList's length:", len(numbers)) # Printing previous list length.
###
del numbers[1] # Removing the second element from the list.
print("New list's length:", len(numbers)) # Printing new list length.
print("nNew list content:", numbers) # Printing current list content.
###
del: Any of the list's elements may be removed
6. Module 2 Lists - collections of data
Negative indices are legal
numbers = [111, 7, 2, 1]
print(numbers[-1])
print(numbers[-2])
1
2
an index -1 -> last one element in the list
7. Module 2 Lists - collections of data
Functions vs. methods
Function method
A method is a specific kind
of function;
Invocvation:
result =
function(arg)
Doesn't belong
to any data
Is owned by the
whole code
Function
Invocation:
result =
data.method(arg)
Is able to change
the state of a
selected entity
Is owned by the
data it works for
Method
8. Module 2 Lists - collections of data
append(), insert()
numbers = [111, 7, 2, 1]
print(len(numbers))
print(numbers)
numbers.append(4)
print(len(numbers))
print(numbers)
numbers.insert(0, 222)
print(len(numbers))
print(numbers)
my_list = [] # Creating an empty list.
for i in range(5):
my_list.append(i + 1)
print(my_list)
• new element
to the end of
the existing list
list.append(value)
• add a new
element at any
place
list.insert(location,
value)
9. Module 2 Lists - collections of data
Making use of lists
my_list = [10, 1, 8, 3, 5]
total = 0
for i in range(len(my_list)):
total += my_list[i]
print(total)
my_list = [10, 1, 8, 3, 5]
total = 0
for i in my_list:
total += i
print(total)
27 27
11. Module 2 Lists - collections of data
Key takeaways
The list is a type of
data used to store
multiple objects.
Lists can be indexed
and updated.
List elements and lists
can be deleted.
Lists can be iterated
through using the
for loop.
The len() function: to
check the list's length Function, Method
12. Module 2 Lists - collections of data
21. The basics of lists
22. The basics of lists - the Beatles
LAB Practice