5 Data Types - Lists
5 Data Types - Lists
(Midterm Test)
1
Data Types-Lists
Objectives
2
Working with Lists
Part - 1
3
Data Types- Lists
Working with Lists
• A List contains a collection of items (data).
• Lists are ordered (indexed) sequences of arbitrary data (items) that is mutable.
• Sequence means that a list stores items in the sequence in which they are added to the list,
lists are a type of data structure known as a sequence.
• Mutable means that the length of the sequence can be changed and elements can be
substituted.
• Lists can be of any data, of one type or mixed type, and can be written explicitly.
• The basic format is a square-bracket-enclosed [ ], comma-separated list of arbitrary data.
4
Data Types- Lists
Working with Lists
• You can also create an empty list. Then, you can add items to this list later on.
• In some languages (like Java), this structure is referred to as an array and the items
in the array are referred to as elements.
• Just like strings, to refer to (and to get) an item in a list, you use an index starting at 0. The
second item has an index of 1, and so on.
• You can also get items starting from the end of the list by using a negative integer as the
index. Then, the last item in a list has an index of -1, the second to last item has an index of
-2, and so on.
• You can get an item in a list and assign its value to a variable.
5
Data Types- Lists
Working with Lists
• You can use the repetition operator (*) to help create a list. For example, you create a list
with one item, then you use the repetition operator to repeat that item as many times as you
want.
• In the same way, you can add an item to a list. Here, you code the name of the list and the
index that accesses the item on the left of the assignment operator (=). Then, you code the
data that you want to assign to the item on the right side of the assignment operator.
• If you attempt to access an item that doesn’t exist in the list, you’ll get an IndexError that
indicates that the index is out of range.
6
Data Types- Lists
Working with Lists: Examples
7
Data Types- Lists
Working with Lists: Examples
8
Data Types- Lists
Working with Lists: slicing lists
9
Data Types- Lists
Working with Lists: Examples for slicing and concatenating lists
10
Data Types- Lists
Working with Lists: copying lists
• The assignment operator makes a shallow copy of a list, so both list variables refer to the
same list. Since a list is a mutable type, this causes both variables to refer to the same list.
As a result, if you use one variable to change the list, those changes are also available to the
other variable.
• By contrast, the deepcopy() function of the copy module makes a deep copy of the list, so
the list variables refer to two different lists.
• You have to import the copy module in order to use the deepcopy() function. As a result,
you can use one variable to change items in one list, and you can use the other variable to
change the items in the other list.
11
Data Types- Lists
Working with Lists: Examples for copying lists
12
Data Types- Lists
Working with Lists: creating a list of lists
• To create a list of lists, you code another list within each item of a first list. This takes what
you learned about lists and applies it to a list of lists.
• To refer to the items in a list of lists, you use two indexes. If necessary, you can also use
negative indexes to work from the end of each list.
• This can also be referred to as a two-dimensional list, and you can think of the data as
columns within rows.
13
Data Types- Lists
Working with Lists: Examples of creating a list of lists
14
Data Types- Lists
Working with Lists: Examples of creating a list of lists
15
Data Types- Lists
16
Data Types- Lists
17
Data Types- Lists
18
Data Types- Lists
19
Data Types- Lists
20
Data Types- Lists
21
Data Types- Lists
22
Data Types- Lists
23
Data Types- Lists
24
Data Types- Lists
25
Data Types- Lists
27
Data Types- Lists
28
Data Types- Lists
29
Data Types- Lists
30
Data Types- Lists
31
Data Types- Lists
32
Data Types- Lists
33
33
Data Types- Lists
34
34
Data Types- Lists
35
35
Data Types- Lists
36
36
References
Material has been taken from (can be accessed through Sheridan's Library Services) :