0% found this document useful (0 votes)
38 views13 pages

List 1

Lists are one of the four built-in data types in Python used to store collections of data. Lists are ordered and changeable, allow duplicate values, and use indexes to access items. Common list methods allow you to append, insert, remove, sort, and loop through items in a list.

Uploaded by

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

List 1

Lists are one of the four built-in data types in Python used to store collections of data. Lists are ordered and changeable, allow duplicate values, and use indexes to access items. Common list methods allow you to append, insert, remove, sort, and loop through items in a list.

Uploaded by

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

Unit 11.

2A: Data Structures


Python Lists

11.2.3.1create a list
11.2.3.2organize the output of a string using the split() and join() methods
11.2.2.1perform access to the elements of strings, lists, tuples
11.2.3.3apply functions and methods of processing lists
11.4.3.2solve applied problems from various subject areas
11.2.3.6 determine the difference between different data structures
mylist = ["apple", "banana", "cherry"]

List
Lists are used to store multiple items in a single variable.

Lists are one of 4 built-in data types in Python used to store collections of
data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and
usage.

Lists are created using square brackets:


thislist = ["apple", "banana", "cherry"]
print(thislist)
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
When we say that lists are ordered, it means that the 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.

Note: There are some list methods that will change the order, but in general: the order of
the items will not change.
Changeable
The list is changeable, meaning that we can change, add, and remove items in a list after it has been
created.

Allow Duplicates
Since lists are indexed, lists can have items with the same value:

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


print(thislist)

List Length List Items - Data Types


list1 = ["apple", "banana", "cherry"]
Print the number of items in the list: list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
thislist = ["apple", "banana", "cherry"] list4 = ["abc", 34, True, 40, "male"]
print(len(thislist))
mylist = ["apple", "banana", "cherry"]
print(type(mylist))

There are four collection data types in the Python programming language:
•List is a collection which is ordered and changeable. Allows duplicate members.
•Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
•Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
•Dictionary is a collection which is ordered** and changeable. No duplicate members.
Python - Access List Items
Print the second item of the list: Note: The first item has index 0.
thislist = ["apple", "banana", "cherry"]
print(thislist[1])

Negative Indexing
Negative indexing means start from the end -1 refers to the last item, -2 refers to the
second last item etc.
thislist = ["apple", "banana", "cherry"]
print(thislist[-1])

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])
Python - Change List Items
Change the second and third value by replacing it with
Change the second item: one value:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant" thislist = ["apple", "banana", "cherry"]
print(thislist) thislist[1:3] = ["watermelon"]
print(thislist)

Change the values "banana" and "cherry" with the values "blackcurrant" and "watermelon":
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)

Change the second value by replacing it with two new values:


thislist = ["apple", "banana", "cherry"]
thislist[1:2] = ["blackcurrant", "watermelon"]
print(thislist)

Note: The length of the list will change when the number of items inserted does not match the
number of items replaced.
Python - Add List Items To append elements from another list to the
current list, use the extend() method.
To add an item to the end of the list, use
the append() method:
Add the elements of tropical to thislist:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange") thislist = ["apple", "banana", "cherry"]
print(thislist) tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)
Insert Items
To insert a list item at a specified index, use the insert()
method.
The insert() method inserts an item at the specified Add Any Iterable
index: The extend() method does not have to append lists, you can add any iterable
object (tuples, sets, dictionaries etc.).

Insert an item as the second position:


Add elements of a tuple to a list:
thislist = ["apple", "banana", "cherry"]
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
thistuple = ("kiwi", "orange")
print(thislist)
thislist.extend(thistuple)
print(thislist)

Note: As a result of the examples above, the lists


will now contain 4 items.
thislist = ["apple", "banana", "cherry"] Clear the list content:
thislist.remove("banana") thislist = ["apple", "banana", "cherry"]
print(thislist) thislist.clear()
print(thislist)

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


thislist.pop(1)
print(thislist) Remove the first item:
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)
If you do not specify the index,
the pop() method removes the last item.

thislist = ["apple", "banana", "cherry"] Delete the entire list:


thislist.pop() thislist = ["apple", "banana", "cherry"]
print(thislist) del thislist
Loop Through a List
You can loop through the list items by using a for loop:

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


for x in thislist:
print(x)

Loop Through the Index Numbers


You can also loop through the list items by referring to their index number.
Use the range() and len() functions to create a suitable iterable.

Print all items by referring to their index number:


thislist = ["apple", "banana", "cherry"]
for i in range(len(thislist)):
print(thislist[i])
Sort List Alphanumerically
List objects have a sort() method that will sort the list alphanumerically, ascending, by default:
Sort the list alphabetically:
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)

Sort the list numerically:


thislist = [100, 50, 65, 82, 23]
thislist.sort()
print(thislist)

Sort the list descending:


thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort(reverse = True)
print(thislist)

thislist = [100, 50, 65, 82, 23]


thislist.sort(reverse = True)
print(thislist)
Copy a List
You cannot copy a list simply by typing list2 = list1, because: list2 will only be a reference to list1, and
changes made in list1 will automatically also be made in list2.
There are ways to make a copy, one way is to use the built-in List method copy().
Make a copy of a list with the copy() method: Make a copy of a list with the list() method:

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


mylist = thislist.copy() mylist = list(thislist)
print(mylist) print(mylist)

Join Two Lists Append list2 into list1:


list1 = ["a", "b" , "c"]
Join two list: list2 = [1, 2, 3]
list1 = ["a", "b", "c"]
list2 = [1, 2, 3] for x in list2:
list1.append(x)
list3 = list1 + list2
print(list3) print(list1)
List Methods
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list

You might also like