We have already started with some of the most basic ideas behind the list
s,
Today we are going to focous on the features, advantages and disadvantages of working with list
s.
On python
, a list
is a set of things, of any kind!, and even each compound can have a different type than the others, you can have,
list
sstring
sint
or float
The only thing we have to consider is to make it inside of []
, let see some examples
list1=[]
print(list1)
type(list1)
[]
list
An empty list
, and
list2=[10]
print(list2,type(list2),type(list2[0]))
[10] <class 'list'> <class 'int'>
Operations such as +
, *
can be performed, but the result is not what one would expect,
list1=[1,2,3,4,5]
list2=[3,2,4,6,9]
print(list1+list2)
[1, 2, 3, 4, 5, 3, 2, 4, 6, 9]
This operation cannot be performed with an number and a list
print(list1+2)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-4-f7df2789d0eb> in <module> ----> 1 print(list1+2) TypeError: can only concatenate list (not "int") to list
We have to take into account that, not all operations are allowed between certain types
print(list1*list2)
Sometimes they work with specific types,
print(list1*2)
Finally
print(list1**2)
So, we have to explore which operators can be used on which variables.
Lets create a list with different kind of data, an int
, float
, character, str
and a list
list_test=[1,2.0,'c',"word",['list_a','list_b']]
Let us explore the data.
for i in list_test:
print(type(i))
look that 'c'
and "word"
are of the same type.
print(list_test)
Let us explore some of the functions we can use on list
s
len()
print(len(list_test))
print(len(list_test[0]))
Note : Doesn't work on numbers, but on strings?
print(len(list_test[3]), list_test[3])
In some sense, the
str
andlist
have the same structure!
for
¶for i in list_test:
print(i)
for i in range(len(list_test)):
print(i, list_test[i])
enumerate
function¶for i,j in enumerate(list_test):
print(i,j)
list
s also can be accesed with negative values!
print(list_test[-1])
Sometimes, you can have more than one index
print(list_test[-1][1][-1])
append
method,¶there are different ways to use functions on structures as lists
, for example len()
, or range()
, but there are some such as append
that are called Methods, they are the heart of python
because is a language based on Object Oriented Programming
print(list_test)
list_test.append(1)
print(list_test)
list_test.append([1,2,3,4,5])
print(list_test)
Look for some methods to erase cells on a
list
You can change the elements of a list, even if the new value have a different type.
list_test[6]=2
print(list_test)
There are other things we can do on list
s, for example, how can we get more than one value of a list
at a time?
print(list_test[1:4])
print(list_test[3][2:])
When we use [2:]
it means that it starts at [2]
and goes until the end. we can also use [:3]
and it means that goes from the begining until the 2nd compound.
A string is a set of characters,
test1='test'
test2="test"
print(test1==test2)
There is no difference between '
, "
.
And we can use the same structure than we just did with the list
s!
len(test1)
print(test1[1],test2[3])
test1.append('a')
test1=test1+'\t'+'test2'
print(test1)
Strings can be multiplyed
print(test2*2,type(test2*2))
How can this be useful?