CS1-FULL-FALL2021
Maíra Marques Samary
Lists
Lists
• In Python a list is a sequentially organized collection of
elements, where each element lives in a fixed position.
• Each item in a list is accessed by an index. An index is
an integer value that goes between 0 and n-1 (n being
the length of the list).
• A list can be used to store various types of data.
How to create a list?
?
Simple
And how we can put data inside the
list?
?
Using the operation ‘append’
Result
The data is stored in the list
in the same order they were
aggregated to the list.
And the data is saved in in
the exact data type they
were created (string, int,
etc.)
… it is also possible to create an non
empty list:
We declare the elements
between the square
brackets, separated by a
comma.
Remember the basic:
When we want to access the value of
a variable we simply put its identifier
in some expression …
When we want to access an element
of a list, we must specify the identifier
of the list variable and the position of
the element that we want.
Inside the []s we put
the index number that
we want to work with
What this program prints?
A. 4 lines with the elements of the list printed in the order
in which they have been declared.
B. 4 lines with the elements of the list printed in reverse
order to which they have been declared.
C. 3 lines with the first three elements of the list, and None
in the last line.
A. 4 lines with the elements of the list printed in the order
in which they have been declared.
B. 4 lines with the elements of the list printed in reverse
order to which they have been declared.
C. 3 lines with the first three elements of the list, and None
in the last line.
What this program prints?
We can also modify (change the value
assigned) to an element of a list
!
We use the operator assign (=), with
the list and index position on the left
side :
How can we know the length of a list?
?
The function len() gives
us the length of a list:
Now, if we have a list with 234892
elements and we want to see if the
value 324 is present, how can we go
through it completely?
list[i] it changes in
each cycle iteration,
since the i value is
increased by 1 in
each cycle
There is also a search function for
lists, it is the index() function
• append(x) – it puts the element x at the end of the list
• insert(i,x) – it puts the element x in the position i of the list,
making all the elements after the i index to move to the
right on the list
• extend(L) – it puts all the elements of the L list to the end of
the actual list, summing up to the actual list len(L) elements
• remove(x) – it removes the first appearance of the element x
of the list, so the list has one less element
• pop() – it removes the last element of the list
• pop(i) – it removes the element on the i position on the list
• index(x) – it returns the index of the first element of the list
that is equals to x
• count(x) – it returns the amount of time the element x
appears on the list
• sort() – it puts all the elements of the list in order
• reverse() – it reverses the order of all the elements on the list