DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1

unique. Python just connects the two together into a new
list, as shown in the following example:


>>> a = [1, 2, 4]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print(c)
[1, 2, 4, 4, 5, 6]

Remember all of the slicing you saw with strings? The
same principles apply here, but instead of having a single
string with each letter being in a bucket, the elements in
the list are the items in the bucket. Don’t forget the rule
about the second number after the colon, which means
“up to but not including.” Here is an example:


Click here to view code image


>>> c= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> c[1:4]
[2, 3, 4]
>>> c[ :-4]
[1, 2, 3, 4, 5, 6]
>>> c[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Table 3-6 describes some of the most common list
methods.


Table 3-6 List Methods

MethodWhat It Does

list.appe
nd(elem
ent)

Adds an element to the end of the list

list.clear
()

Removes everything from the list
Free download pdf