You are on page 1of 16

Python Lists

Lecture 4
Lists
• Python has a great built-in list type named "list". List literals are
written within square brackets [ ]. Lists work similarly to strings -- use
the len() function and square brackets [ ] to access data, with the first
element at index 0.

• colors = ['red', 'blue', 'green']


print(colors[0]) ## red
print(colors[2]) ## green
print(len(colors)) ## 3
Lists

• List items are ordered, changeable, and allow duplicate values.

• The "empty list" is just an empty pair of brackets [ ]. The '+' works to
append two lists, so [1, 2] + [3, 4] yields [1, 2, 3, 4] (this is just like +
with strings).
List Length

• To determine how many items a list has, use the len() function:

thislist = ["apple", "banana", "cherry"]


print(len(thislist))
Access List Items
• Print the second item of the list:

• thislist = ["apple", "banana", "cherry"]


• Negative Indexing
• Negative indexing means start from the end

• -1 refers to the last item, -


Range of Indexes

• You can specify a range of indexes by specifying where to start and


where to end the range.

• When specifying a range, the return value will be a new list with the
specified items.
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon",
"mango"]
print(thislist[2:5])
Add List Items
• To add an item to the end of the list, use the append() method:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")

Insert Items
To insert a list item at a specified index, use the insert() method.
The insert() method inserts an item at the specified index:
Insert an item as the second position:
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
Extend List

• To append elements from another list to the current list, use the
extend() method.

• Add the elements of tropical to thislist:

thislist = ["apple", "banana", "cherry"]


tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)
Remove List Items
• Remove Specified Item
• The remove() method removes the specified item.

• Remove "banana":

thislist = ["apple", "banana", "cherry"]


thislist.remove("banana")
print(thislist)
• If there are more than one item with the specified value, the
remove() method removes the first occurance:
thislist = ["apple", "banana", "cherry", "banana", "kiwi"]
thislist.remove("banana")
print(thislist)
Remove Specified Index

• The pop() method removes the specified index.

• Remove the second item:

thislist = ["apple", "banana", "cherry"]


thislist.pop(1)
print(thislist)
• If you do not specify the index, the pop() method removes the last
item.

• Remove the last item:

thislist = ["apple", "banana", "cherry"]


thislist.pop()
print(thislist)
Clear the List

• The clear() method empties the list.

• The list still remains, but it has no content.


thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
FOR and IN
• Python's *for* and *in* constructs are extremely useful, and the first use of
them we'll see is with lists. The *for* construct -- for var in list -- is an easy
way to look at each element in a list (or other collection)

squares = [1, 4, 9, 16]


sum = 0
for num in squares:
sum += num
sum= sum+num
print(sum/len(squares)) ## 30/4=
In keyword
• The *in* construct on its own is an easy way to test if an element
appears in a list (or other collection) -- value in collection -- tests if the
value is in the collection, returning True/False.
list = ['larry', 'curly', 'moe']
if 'curly' in list:
print('yay') ## yay
Exercise
• Create a empty list called evenNumbers and add even numbers
between 2 and 50 in list.
• evenNumber=[]
• Then print list

You might also like