Python List
Python List
Samir V
Python’s list is a flexible, versatile, powerful, and popular built-in data type.
Lists are sequences of objects. They’re commonly called containers or
collections because a single list can contain or collect an arbitrary number of
other objects.
It allows you to create variable-length and mutable sequences of objects. In a
list, you can store objects of any type. You can also mix objects of different
types within the same list, although list elements often share the same type.
slide 2
Samir V
In Python, lists are ordered, which means that they keep their elements in
the order of insertion
print( colors)
['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet’]
You can access an individual object in a list by its position or index in the
sequence. Indices start from zero.
Print(colors[0])
'red'
Print(colors[2])
'yellow'
slide 3
Samir V
Lists can contain objects of different types. Though it is great feature of list,
but one must avoid using different data types in lists.
Mylist = [42, "apple", True, {"name": "John Doe"}, (1, 2, 3), [3.14, 2.78]]
Creating Lists
Eg –
digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
fruits = ["apple", "banana", "orange", "kiwi", "grape"]
slide 4
Samir V
Accessing Items in a List:
Indexing -
You can access individual items from a list using the item’s
associated index. An index start at 0 and go up to the
number of items in the list minus 1.
To access a list item through its index, you use the following
syntax:
list_object[index]
You can also use negative indices while indexing lists. This kind of index gives
you access to the list items in backward order.
Eg –
Print(languages[-1])
'Rust’
slide 5 Print(languages[-4]) Samir V
Retrieving Multiple Items From a List:
Slicing-
Another common requirement when working with lists is to
extract a portion, or slice, of a given list. You can do this with
a slicing operation, which has the following syntax:
list_object[start:stop:step]
ul = letters[0::2] # Or [::2]
Print(ul)
['A', 'B', 'C', 'D']
slide 6
Samir V
digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
first_three = digits[:3]
Print(first_three)
[0, 1, 2]
last_three = digits[-3:]
Print(last_three)
[7, 8, 9]
every_other = digits[::2]
Print(every_other)
[0, 2, 4, 6, 8]
slide 7
Samir V
Note - out-of-range values for start and stop don’t cause slicing expressions
to raise a TypeError exception.
If start is before the beginning of the list, which can happen when you use
negative indices, then Python will use 0 instead.
If start is greater than stop, then the slicing will return an empty list.
If stop is beyond the length of the list, then Python will use the length of the
list instead.
Print( len(colors))
7
Print(colors[-8:])
['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet’]
Print( colors[8:])
[]
slide 8
Samir V
Aliases of a List
In Python, you can create aliases of variables using the assignment operator
(=).
Assignments don’t create copies. Instead, they create bindings between the
variable and the object involved in the assignment. In other words they create
another name for the same variable. Therefore, when you have several aliases
of a given list, changes in an alias will affect the rest of the aliases.
mylist =[1,2,3,4,5]
ml=mylist
Print(mylist)
print(ml)
mylist[2]=8
print(mylist)
print(ml)
slide 9
Samir V
Updating Items in Lists:
Python lists are mutable data types. This means that you can change their
elements without changing the identity of the underlying list. They allow you
to update the value of one or more items in an existing list.
numbers = [1, 2, 3, 4]
numbers[0] = "one"
Print( numbers)
['one', 2, 3, 4]
numbers[-1] = "four"
Print( numbers)
['one’, 2, 3, 'four']
slide 10
Samir V
Traversing Lists
To traverse a list, you’ll need a loop that goes over each element from the start
to the end of the list.
We will use for loops.
red
orange
yellow
green
blue
indigo
violet
slide 11
Samir V
You may use index of list to access its elements-
for i in range(len(colors)):
print(colors[i])
red
orange
yellow
green
blue
indigo
violet
slide 12
Samir V
List Methods -
list.append(val)
Add an item to the end of the list.
list.insert(i, val)
Insert an item at a given position. The first argument is the index of the
element before which to insert, so a.insert(0,val) inserts at the front of the list.
list.remove(val)
Remove the first item from the list whose value is equal to val. It raises a
ValueError if there is no such item.
list.pop([i])
Remove the item at the given position in the list, and return it.
If no index is specified, it removes and returns the last item in the list. It raises
an IndexError if the list is empty or the index is outside the list range.
list.clear()
Remove all items from the list.
slide 13
Samir V
list.index(val , start, end)
Return zero-based index in the list of the first item whose value is equal to val.
Raises a ValueError if there is no such item.
The optional arguments start and end are used to limit the search to a
particular subsequence of the list.
list.count(val)
Return the number of times val appears in the list.
list.reverse()
Reverse the elements of the list in place.
list.copy()
Return a shallow copy of the list.
slide 14
Samir V