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

List

Python fyup 5th semester
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)
18 views13 pages

List

Python fyup 5th semester
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/ 13

In Python, objects are categorized as either mutable or immutable

based on whether their state can be changed after creation.


Mutable Objects:
• Definition: Mutable objects can be modified after they are
created. This means their content or state can be altered
without creating a new object in memory.
• Examples: Lists, dictionaries, sets, byte arrays.
• Characteristics:
o Operations like adding, removing, or changing elements
modify the object in place.
o Changes to a mutable object are reflected wherever that
object is referenced.
• Use Cases: Useful when the data needs to be frequently
updated or extended.
Immutable Objects:
• Definition:
Immutable objects cannot be modified after they are created. Any
operation that appears to "change" an immutable object actually
results in the creation of a new object with the desired changes.
• Examples:
Integers, floats, strings, tuples, booleans, frozensets.
• Characteristics:
• Their value is fixed once assigned.
• When an operation seemingly modifies an immutable
object, a new object with the new value is created, and
the variable referencing it is updated to point to the new
object.
Python Lists
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:
Example
Create a List:
thislist = ["apple", "banana", "cherry"]
print(thislist)

In Python, both lists and arrays are used to store


collections of data, but they differ significantly in their
characteristics and use cases.
Lists:
• Heterogeneous:
Lists can store elements of different data types within
the same list (e.g., integers, strings, floats).
Arrays (from the array module or NumPy arrays):
• Homogeneous:
Arrays from the array module store elements of a
single, fixed data type.

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.
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:
Example
Lists allow duplicate values:
thislist =
["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)

List Length
To determine how many items a list has, use
the len() function:
Example
Print the number of items in the list:
thislist = ["apple", "banana", "cherry"]
print(len(thislist))

List Items - Data Types


List items can be of any data type:
Example
String, int and boolean data types:
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]

A list can contain different data types:


Example
A list with strings, integers and boolean values:
list1 = ["abc", 34, True, 40, "male"]

Access Items
List items are indexed and you can access them by
referring to the index number:
Example
Print the second item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
Print the last item of the list:
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.
Example
Return the third, fourth, and fifth item:
thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon"
, "mango"]
print(thislist[2:5])

By leaving out the end value, the range will go on to


the end of the list:
Example
This example returns the items from "cherry" to the
end:
thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon"
, "mango"]
print(thislist[2:])
By leaving out the start value, the range will start at the
first item:
Example
This example returns the items from the beginning to,
but NOT including, "kiwi":
thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon"
, "mango"]
print(thislist[:4])

Change Item Value


To change the value of a specific item, refer to the
index number:
Example
Change the second item:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
Change a Range of Item Values
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
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)

Append Items
To add an item to the end of the list, use
the append() method:
Example
Using the append() method to append an item:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
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
index:
Example
Insert an item as the second position:
thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "orange")
print(thislist)
Note: As a result of the examples above, the lists will
now contain 4 items.

Extend List
To append elements from another list to the current
list, use the extend() method.
Example
Add the elements of tropical to thislist:
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)

Remove Specified Item


The remove() method removes the specified item.
Example
Remove "banana":
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
Remove the last item:
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)

Delete the entire list:


thislist = ["apple", "banana", "cherry"]
del thislist
Loop Through a List
Print all items in the list, one by one:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
Sort List Alphanumerically
List objects have a sort() method that will sort the list
alphanumerically, ascending, by default:
Example
Sort the list alphabetically:
thislist =
["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)

Sort Descending
To sort descending, use the keyword argument reverse
= True:
Example
Sort the list descending:
thislist =
["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort(reverse = True)
print(thislist)

Use the copy() method


You can use the built-in List method copy() to copy a
list.
Example
Make a copy of a list with the copy() method:
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)

Join Two Lists


There are several ways to join, or concatenate, two or
more lists in Python.
One of the easiest ways are by using the + operator.
Example
Join two list:
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

list3 = list1 + list2


print(list3)

You might also like