Professional Documents
Culture Documents
LISTS
List is a sequence of values.
In [2]:
In [ ]:
In [3]:
friends
Out[3]:
In [4]:
print(friends)
In [5]:
list1 = [1,'hi']
In [6]:
list1
Out[6]:
[1, 'hi']
In [7]:
a = [1,2,[3,4]]
In [8]:
Out[8]:
In [41]:
x = "hello"
x[0]='b'
--------------------------------------------------------------------------
-
TypeError Traceback (most recent call las
t)
<ipython-input-41-24e7765a4a05> in <module>
1 x = "hello"
----> 2 x[0]='b'
The syntax of accessing the elemets of a list is same as for accessing the characters of a string: The bracket
operator.
In [1]:
#string
x = "hello"
print(x[0])
In [22]:
cheese= ['bread','mango']
print(cheese[0])
bread
Unlike strings, Lists are mutable because you can change the order of items in list or reassign an item in a
list.
When the bracket operator on the left side of an assignment, it identifies the element of the list that will
be assigned.
In [ ]:
In [12]:
[17, 5]
List is a relationship between indices and elements . This relationship is called a mapping.
In [16]:
Out[16]:
True
In [ ]:
Traversing a list
The most common way to traverse the elements of a list is with a for loop
In [24]:
['bread', 'mango']
['bread', 'mango']
In [ ]:
The len() function takes a list as a parameter and returns the number of elements in the list
In [2]:
for x in []:
print("This never happens")
In [28]:
x = ['spam',1,'end',[1,2,3]]
x
Out[28]:
List Operations
In [38]:
a = [1,2,3]
b = [4,5,6]
c = a+b
c
Out[38]:
[1, 2, 3, 4, 5, 6]
In [39]:
[0]*4
Out[39]:
[0, 0, 0, 0]
In [40]:
[0]*8
Out[40]:
[0, 0, 0, 0, 0, 0, 0, 0]
In [41]:
[1,2,3]*3
Out[41]:
[1, 2, 3, 1, 2, 3, 1, 2, 3]
List Slices
In [45]:
t = ['a', 'b','c','d','e','f']
t[:6]
Out[45]:
If you omit the first index , the slice starts at the beginning. If you omit the second, the slice goes to the
end.
In [48]:
t[1:3]
Out[48]:
['b', 'c']
In [49]:
t[:]
Out[49]:
In [ ]:
A slice operator on the left side of an assignment can update multiple elements
In [50]:
In [ ]:
List methods
Append method
In [51]:
t = ['a','b','c']
t.append('d')
print(t)
In [ ]:
Extend method
In [52]:
t1 = ['a','b','c']
t2 = ['d','e']
t1.extend(t2)
print(t1)
Sort Method
In [53]:
t = ['d','c','e','b','a']
t.sort()
print(t)
Deleting Elements
In [44]:
t = ['a','b','c']
x =t.pop(1)
print(t)
['a', 'c']
In [45]:
print(x)
Pop modifies the list and return the element that was removed.
In [46]:
x =t.pop()
In [47]:
print(x)
In [48]:
Out[48]:
['a']
Note: If you don't need the removed value , you can use the "del" operator
In [60]:
t = ['a','b','c']
del t[1]
print(t)
['a', 'c']
Note: If you know the element you want to remove (not index) , you can use remove
In [61]:
t = ['a','b','c']
t.remove('b')
print(t)
['a', 'c']
To remove more than one element , you can use del with a slice index
In [62]:
t = ['a','b','c','d','e','f']
del t[1:5]
print(t)
['a', 'f']
In [64]:
nums = [3,41,12,9,74,15]
#len function
print(len(nums))
In [65]:
#maximum
print(max(nums))
74
In [66]:
#Minimum
print(min(nums))
In [68]:
#Sum
print(sum(nums))
#Note: sum function only works when the list elements are numbers
154
In [49]:
total = 0
count = 0
while(True):
inp = input('enter a number: ')
if inp == 'done' :break
value = float(inp)
total = total +value
count = count+1
average = total/count
print('average :', average )
enter a number: 4
enter a number: 5
enter a number: 6
enter a number: done
average : 5.0
Use built-in functions to compute the sum and count at the end
In [20]:
enter a number3
enter a number4
enter a number5
enter a number6
enter a numberdone
average 4.5
A string is sequence of characters and a list is a sequence of values , but a list of characters is not the same
as a string
In [77]:
s = 'spam'
t = list(s)
print(t)
List is the name of built in function. It breaks a string into individual letters. If you want to break a string into
words, use split method
In [21]:
In [22]:
print(t[2])
You can call split with optional argument called a delimiter: that specifies which character to use as word
boundaries.
In [80]:
s = 'spam-spam-spam'
delimiter = '-'
s.split(delimiter)
Out[80]:
In [51]:
t = ['hello','world','!']
delimiter = ' '
delimiter.join(t)
# Note : In this case the delimiter is a space character, so join puts a space between
words.
# To concatenate strings without spaces, you can use the empty string: " " , as a delim
iter
Out[51]:
In [83]:
a = 'banana'
b = 'banana'
We know that a and b both refer to a string, but we don’t know whether they refer to the same string. There
are two possible states:
In one case, a and b refer to two different objects that have the same value. In the second case, they
refer to the same object.
To check whether two variables refer to the same object, you can use the is operator.
In [84]:
a is b
Out[84]:
True
In this example, Python only created one string object, and both a and b refer to it. But when you create two
lists, you get two objects:
In [85]:
a = [1,2,3]
b = [1,2,3]
a is b
Out[85]:
False
In this case we would say that the two lists are equivalent, because they have the same elements, but
not identical, because they are not the same object. If two objects are identical, they are also
equivalent, but if they are equivalent, they are not necessarily identical
Until now, we have been using “object” and “value” interchangeably, but it is more precise to say that an
object has a value.
If you evaluate [1, 2, 3], you get a list object whose value is a sequence of integers.
If another list has the same elements, we say it has the same value, but it is not the same object.
Aliasing
If a refers to an object and you assign b= a, then both variables refer to the same object
In [27]:
a = [1,2,3]
b = a
b is a
Out[27]:
True
If the aliased object is mutable , changes made with one alias affect the other
In [28]:
b[0] = 17
a
Out[28]:
[17, 2, 3]
List Arguments
When you pass a list to a funtion , the function gets a reference to the list.
In [32]:
def delete_head(t):
del t[0]
letters = ['a','b','c']
delete_head(letters)
print(letters)
['b', 'c']
It is important to distinguish between the operations that modify the lists and operations that create new lists.
In [33]:
t1 = [1,2]
t2 = t1.append(3)
t1
Out[33]:
[1, 2, 3]
In [34]:
t2
In [36]:
t3 = t1 + [4]
print(t3)
[1, 2, 3, 4]
In [37]:
t2 is t3
Out[37]:
False
In [ ]: