You are on page 1of 6

Chapter 9 List & String Selecting many items from a list (or Slicing)

The list is a most versatile datatype available in Python which can be written as a C[start : stop] is a slice of a list C starting from start index and finish just before
list of comma-separated values (items) between square brackets. stop index (the item at the stop index is not included in the slice).

Creating a list is as simple as putting different comma-separated values between


square brackets. For example:

Please note that the good thing about the Python’s list is that items in a list need
not all have the same type and the main point that Python’s list is different from We can also use the negative index in the slicing process too
the other language’s array is that we don’t have to specify the number of element
in advance. So the list is dynamics and very memory efficient.

Accessing Values in Lists (Indexing)

From left to right: the first item in the list has an index 0, the second index is 1, If you want to slice from the first element of the list, you can either specify the
and the next one has index 2 and so forth. starting point for the slice to be 0 or just leave it blank.
From right to left: the first item in the list has an index -1, the second index is -2,
and the next one has index -3 and so forth.

Similarly you can leave the stop index blank in case you want your slice to the last
element of the list.

C[:], it means all the elements in list A will be included in the sliced.
String manipulation Delete List Elements:

 A string works in similar way to a list. In many situations we can consider  You can use either the del statement if you know exactly which element
a string as a list of characters. you are deleting
 Because lists are sequences, indexing and slicing work the same way for  The remove( )method to remove the item that has the matching value.
lists as they do for strings.  The method clear( )will remove everything from the list.

Similarly, the slicing also works for the string.


Note that these functions cannot be applied to a string so you cannot del or
remove character from a string.

Updating Lists:

Append New List Elements:

To add a list element, you can use the append( )method. The new element will be
Be careful, you cannot change the items that don’t exist.
added at the end of the list.

And be careful, String object does not support item assignment as the list.

Given A = “Superman” and if you try A[3] = ‘X’, that will generate an error.
Basic List Operations:

List Function and Method:

There are many useful functions that can be used to manipulate a list or item in
the list.

1. sum(listname) finds summation of all items in the list.

2. len(listname) gives the total length (or total number of elements) of the list

3. max(listname) returns the item that has maximum value in the list

List Of Lists: 4. min(listname) returns the item that has minimum value in the list

List can be nested, meaning that we can have many lists inside a list in the same
way that we have many elements in a list.

C[0]does mean the first element of C which, in fact, is a list A.

And then C[0][0]will mean the first item in that list, which is a number 10.

C[1][0] means Superman.

C[0][1] is 20
To use LIST with FOR, IF and RANDOM:

With the FOR

There are 2 ways to run through all the elements in the list with FOR loop.

The first one uses list in the same way as we used range in the FOR loop earlier.

The second way is that we run through the list with the list’s index.

len(A) was used to find the number of elements automatically. i%2==0can be true
only when i is an even number.
With the IF Review Exercise

You can check the membership of a list easily with the in command. 1. Refer to the the code below:

a_list = [10, 20, 30, 40].

What is the value of a_list[-1]?

2. What does a list mean in Python?

a. A random integer

b. A program that you can use to execute another program and analyze its run-
time behavior

c. A sequence of values

d. A system function

These 2 programs are doing exactly the same thing with different mechanisms.

With the RANDOM 3. How do you access a list element?

RANDOM module has a specific function to random an element out of a list. That a. By the name of the list followed by the index value in brackets.
function is random.choice(listname).
b. By the name of the list followed by a dot and then the index value

c. By the index value followed by a dot and then the name of the list

d. By the name follow by the element’s name

e. no correct answer
4. What is the index value of the first element from the left? 7. Which of the following will NOT produce an error?

a.

a = [1,2,3]

5. What does a Two-Dimensional List do? a.append[4]

a. It forms a tabular, two dimensional arrangement. b.

b. It creates two lists at once. a = "123"

c. It creates a list with only two elements a[3] = 4

d. no correct answer c.

a = [1,2,3]

6. How do you access a list element? a.append(4)

a. variable = name[i] d.

b. variable = i.name; a=[1,2,3]

c. variable = name.get(i); a[3] = 4

d. no correct answer

You might also like