0% found this document useful (0 votes)
15 views43 pages

8.1 List

Lists are a sequence data type that can store collections of data. Lists allow ordered, changeable, and duplicate values. Common list operations include accessing and modifying items by index, slicing lists, adding and removing items, sorting lists, and more.

Uploaded by

Peepeh Ette
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views43 pages

8.1 List

Lists are a sequence data type that can store collections of data. Lists allow ordered, changeable, and duplicate values. Common list operations include accessing and modifying items by index, slicing lists, adding and removing items, sorting lists, and more.

Uploaded by

Peepeh Ette
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

List

List

• List is a sequence data type which is used to store the collection of


data.
• List is a collection of things, enclosed in [ ] and separated by
commas.
• A single list may contain data types like integers, floating point
values, strings, as well as objects.
Creating a List

Syntax

list_name=[value1, value2, …., valuen]

Example

data = [1, 4.5, ‘demo’]


List Items
• List items are ordered, changeable, and allow duplicate values.
• List items are indexed, the first item has index [0], the second item has
index [1] etc.

Ordered
• Items have a defined order, and that order will not change.
• If you add new items to a list, the new items will be placed at the end
of the list.
List Items
Changeable/ Mutable
• The list is changeable, meaning that we can change, add, and remove
items in a list after it has been created.

Duplicate
• Lists can have items with the same value
List Items
Example
list1 = [1,3,4,5,1]
list2 = [3.4, 5.6, ‘text’]
list3 = [ True, False, True ]
print(list1)
print(list3)
print(list3)
List Length

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

Example
data=[1, 3, 2, 5,1]
print(len(data))

Output
5
type( )

• To determine the type of an object, use the type( ) function.

Example
data=[1, 3, 2, 5,1]
print(type(data))

Output
<class ‘list’>
list( )
• It is also possible to use the list() constructor when creating a new list.

Example
data_set = list( (1, 3, 7.65, ‘str’ ) )
print(type(data_set))
print(data_set)
Output
<class ‘list’>
[1, 3, 7.65, ‘str’ ]
Access the list items
• List items are indexed and you can access them by referring to the index
number
• The first item has index 0.

Example
data_set = [1, 3, 7.65, ‘str’ ]
print(data_set[0])
print(data_set[3])
Output
1
‘str’
Access the list items
Negative Indexing
• Negative indexing means start from the end
• -1 refers to the last item, -2 refers to the second last item etc.
Example
color_set = [‘red’, ‘green’, ‘blue’]
print(data_set[-1])
print(data_set[-2])
Output
blue
green
Access the list items
Slicing/ 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

[ start_index : end_index]
Access the list items
Slicing

Example
colors = [ ‘red’, ‘green’, ‘blue’, ‘yellow’, ‘violet’ ]
print(colors[2:4])
# search will start at index 2 (included) and end at index 4 (not included)

Output
[‘blue’, ‘yellow’]
Access the list items
Slicing

Example
colors = [ ‘red’, ‘green’, ‘blue’, ‘yellow’, ‘violet’ ]
print(colors[2:])
# search will start at index 2 (included) and end at last index

Output
[‘blue’, ‘yellow’, ‘violet’]
Access the list items
Slicing

Example
colors = [ ‘red’, ‘green’, ‘blue’, ‘yellow’, ‘violet’ ]
print(colors[:3])
# search will start at index 0 (included) and end at index 3(not included)

Output
[‘red’, ‘green’, ‘blue’]
Access the list items
Slicing
Example
colors = [ ‘red’, ‘green’, ‘blue’]
print(colors[:])
# from beginning to end
# search will start at index 0 (included) and end at index 2(included)

Output
[‘red’, ‘green’, ‘blue’]
Access the list items

Negative Indexes
x=[‘a’, ‘b’, ‘c’, ‘d’]

Index : 0 1 2 3

a b c d
Negative
Index : -4 -3 -2 -1
Access the list items
Slicing using Range of Negative Indexes

Example
colors = [ ‘red’, ‘green’, ‘blue’, ‘yellow’, ‘violet’ ]
print(colors[-3:-1])
# search will start at index -3 (included) and end at index -1(not included)

Output
[‘blue’, ‘yellow’]
Access the list items
Slicing using Range of Negative Indexes

Example
colors = [ ‘red’, ‘green’, ‘blue’, ‘yellow’, ‘violet’ ]
print(colors[-3:])
# search will start at index -3 (included) and end at index -1(included)

Output
[‘blue’, ‘yellow’, ‘violet’]
Access the list items
Slicing using Range of Negative Indexes

Example
colors = [ ‘red’, ‘green’, ‘blue’, ‘yellow’, ‘violet’ ]
print(colors[:-3])
# search will start at index -5/0 (included) and end at index -3(not included)

Output
[‘red’, ‘green’]
Nested list
List inside another list

Example
list_example = [ ‘red’, [1, 2, 3.4] ]
print(list_example[0])
print(list_example[0][1])
print(list_example[1])
print(list_example[1][1])
Nested list
List inside another list

Example
list_example = [ ‘red’, [1, 2, 3.4] ] Output
print(list_example[0]) ‘red’
print(list_example[0][1]) ‘e’
print(list_example[1]) [1, 2, 3.4]
print(list_example[1][1]) [2]
Check if an item exists in a list
Membership Operators : in, not in
Example
colors = [ ‘red’, ‘green’, ‘blue’, ‘yellow’, ‘violet’ ]
if ‘red’ in colors:
print(“Member”)
else:
print(“Not a member”)

Output
Member
Changing an item value
To change the value of a specific item, refer to the index number

Example
colors = [ ‘red’, ‘yellow’, ‘blue’, ‘violet’ ]
colors[1] = ‘green’
print(colors)

Output
[ ‘red’, ‘green’, ‘blue’, ‘violet’ ]
Changing a range of item value
To change the value of items within a specific range,
• Define a list with the new values, and
• Refer to the range of index numbers where you want to insert the
new values
Example
colors = [ ‘red’, ‘yellow’, ‘blue’, ‘violet’ ]
colors[1:3] = [‘green’, ‘magenta’]
print(colors)

Output
[ ‘red’, ‘green’, ‘magenta’, ‘violet’ ]
Changing a range of item value
If you insert more items than you replace, the new items will be inserted
where you specified, and the remaining items will move accordingly.

Example
colors = [ ‘red’, ‘yellow’, ‘blue’]
colors[1:2] = [‘green’, ‘magenta’]
print(colors)

Output
[ ‘red’, ‘green’, ‘magenta’, ‘blue’ ]
Changing a range of item value
If you insert less items than you replace, the new items will be inserted
where you specified, and the remaining items will move accordingly.

Example
colors = [ ‘red’, ‘green’, ‘yellow’, ‘blue’]
colors[1:3] = [‘magenta’]
print(colors)

Output
[ ‘red’, ‘magenta’, ‘blue’ ]
Changing a range of item value
If you insert less items than you replace, the new items will be inserted
where you specified, and the remaining items will move accordingly.

Example
colors = [ ‘red’, ‘green’, ‘yellow’]
colors[1:3] = [‘magenta’]
print(colors)

Output
[ ‘red’, ‘magenta’]
List Methods
Adding items to a list
append( )
To add an item to the end of the list
Syntax: list_name.append(value) # takes only one value
Example
num = [1, 23, 78, 96, 44]
num.append(59)
print(num)

Output
[1, 23, 78, 96, 44, 59]
Adding items to a list
append( )

Example
num_list1 = [1, 23, 78]
num_list2 = [89,97]
num_list1.append(num_list2)
print(num_list1)
Output
[1, 23, 78, [89, 97] ]
Adding items to a list
insert( )
To insert a list item at a specified index
Syntax: list_name.insert( index, value)

Example
num = [1, 23, 78, 96, 44]
num.insert(2, 35)
print(num)

Output
[1, 23, 35, 78, 96, 44, 59]
Adding items to a list
extend( )
To append elements from another list to the current list
Syntax: list_name.extend(list_name)
Example
num_list1 = [1, 23, 78]
num_list2 = [89,97]
num_list1.extend(num_list2)
print(num_list1)
Output
[1, 23, 78, 89, 97]
Removing items from a list
remove( )
removes the specified item
Syntax: list_name.remove(value)

Example
num_list1 = [1, 23, 78]
num_list1.remove(23)
print(num_list1)
Output
[1, 78]
Removing items from a specified index
pop( )
• When index is mentioned, removes the item at specified index
• Otherwise, removes the last item
Syntax: list_name.pop( ) or list_name.pop(index)

Example 1
num_list1 = [1, 23, 78]
num_list1.pop( ) #index is not passes as an argument, removes last item
print(num_list1)
Output
[1, 23]
Removing items from a specified index
pop( )
• When index is mentioned, removes the item at specified index
• Otherwise, removes the last item
Syntax: list_name.pop( ) or list_name.pop(index)

Example 2
num_list1 = [12, 23, 78]
num_list1.pop(1) #index is passed, removes item at index 2
print(num_list1)
Output
[12, 78]
Removing items from a specified index
del
• Used to delete an object
• Can also be used to delete an item at a specified index in a list
Syntax: del list_name[index]

Example 1
num_list1 = [12, 23, 78]
del num_list1[1]
print(num_list1)
Output
[12, 78]
Removing items from a specified index
del
• Used to delete an object
• Can also be used to delete an item at a specified index in a list
Syntax: del list_name
Example 2
num_list1 = [12, 23, 78]
del num_list1
print(num_list1)
Output
name ‘num_list1’ is not defined
Removing items from a specified index
clear( )
Empties the list
Syntax: list_name.clear( )

Example
num_list1 = [12, 23, 78]
num_list1.clear( )
print(num_list1)
Output
[]
Counting the occurrence of an item
count( )
returns the count of the occurence of item passed as an argument
Syntax: list_name.count(value )

Example
num_list1 = [12, 23, 78, 12]
print(num_list1.count(12 ))

Output
2
Reversing a list
reverse( )
reverse the order of items in a list
Syntax: list_name.reverse( )

Example
num_list1 = [12, 23, 78, 2]
num_list1.reverse( )
print(num_list1)

Output
[2, 78, 23, 12]
Sorting a list
sort( )
sort the list items in ascending order (by default )
Syntax: list_name.sort(reverse=True | False, key)
reverse, key – optional arguments
Example 1
num_list1 = [12, 23, 78, 2]
num_list1.sort( )
print(num_list1)

Output
[2, 12, 23, 78]
Sorting a list
sort( )
sort the list items in ascending order (by default )
Syntax: list_name.sort(reverse=True | False, key)
reverse = True – Descending, False - Ascending
Example 2
num_list1 = [12, 23, 78, 2]
num_list1.sort( reverse=True)
print(num_list1)

Output
[78, 23, 12 , 2]

You might also like