0% found this document useful (0 votes)
31 views39 pages

2 Python List, Tuple, Set 05-10-2022

Uploaded by

srajgiri40
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)
31 views39 pages

2 Python List, Tuple, Set 05-10-2022

Uploaded by

srajgiri40
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/ 39

Python Lists

• Lists are used to store multiple items in


a single variable.
• Lists are created using square brackets:
• Create a List:
thislist =
["apple", "banana", "cherry"]
print(thislist)
• 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.
Note1 :When we say that lists are ordered, it means that
the items have a defined order, and
If you add new items to a list, the new items will be placed
at the end of the list.
Note2 : The list is changeable, meaning that we can
change, add, and remove items in a list after it has been
created.
• letters =
'abcdefghijklmnopqrstuvwxyzABCDEFGH
IJKLMNOPQRSTUVWXYZ'

• string_letters = str(letters)
• lists_letters = list(letters)
• tuples_letters = tuple(letters)
• sets_letters = set(letters)
• print("String: ", string_letters)
• print() # for new line
• print("Lists: ", lists_letters)
• print() # for new line
• print("Tuples: ", tuples_letters)
• print() # for new line
• print("Sets: ", sets_letters)
• Allow Duplicates
• Lists allow duplicate values:
thislist =
["apple", "banana", "cherry", "apple", "cher
ry"]
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 and
different datatypes :
list1 =
["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
• 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])
Negative Indexing
• Negative indexing means start from the
end
• -1 refers to the last item, -2 refers to the
second last item etc.
• Example
• 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.
• Return the third, fourth, and fifth item:
thislist = ["apple", "banana", "cherry", "orange",
"kiwi", "melon", "mango"]
print(thislist[2:5])
• index 2 (included) and end at index 5 (not
included).
thislist = ["apple", "banana", "cherry",
"orange", "kiwi", "melon", "mango"]
print(thislist[:4]) # from 0th to 3rd
thislist = ["apple", "banana", "cherry",
"orange", "kiwi", "melon", "mango"]
print(thislist[2:]) #from 3rd to end
• Range of Negative Indexes

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


"kiwi", "melon", "mango"]
print(thislist[-4:-1])
thislist =
["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the
fruits list")
Change Item Value

• Change the second item:


thislist =
["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
Change a Range of Item
Values
thislist =
["apple", "banana", "cherry", "orange",
"kiwi", "mango"]
thislist[1:3] =
["blackcurrant", "watermelon"]
print(thislist)
Output
'apple', 'blackcurrant', 'watermelon', 'orange', 'kiwi', 'mango']
thislist = ["apple", "banana", "cherry"]
thislist[1:2] = ["blackcurrant", "watermelon"]
print(thislist)

'apple', 'blackcurrant', 'watermelon',


[

'cherry']
• thislist =
["apple", "banana", "cherry"]
thislist[1:3] = ["watermelon"]
print(thislist)

['apple', 'watermelon']
• Insert Items
• To insert a new list item, without replacing
any of the existing values, we can use the
insert() method.
• The insert() method inserts an item at the
specified index:
thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "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)
Extend List
• To append elements from another list to
the current list, use the extend() method.
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.

Remove "banana":

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


• thislist.remove("banana")
• print(thislist)
Remove Specified Index
• The pop() method removes the specified
index.
• Remove the second item:
• thislist = ["apple", "banana", "cherry"]
• thislist.pop(1)
• print(thislist)
Note: If you do not specify the index, the
pop() method removes the last item.
Remove the last item:

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


• thislist.pop()
• print(thislist)
• The del keyword also removes the
specified index:
• Example
• Remove the first item:

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


• del thislist[0]
• print(thislist)
• The del keyword can also delete the list
completely.

• Example
• Delete the entire list:

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


• del thislist
• Clear the List
• The clear() method empties the list.
• The list still remains, but it has no
content.
• Example
• Clear the list content:
• thislist = ["apple", "banana", "cherry"]
• thislist.clear()
• print(thislist)
Python - 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
• List = ['Mathematics', 'chemistry', 1997,
2000]
• # Insert at index 2 value 10087
• List.insert(2,10087)
• print(List)
Calculates total occurrence
of a given element of List.
• List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
• print(List.count(1))
• List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
• print(len(List))
index()
• Returns the index of the first
occurrence. The start and End index
are not necessary parameters.
• List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
• print(List.index(2))
remove()
• Element to be deleted is mentioned using
list name and element.
• Syntax
• list.remove(element)
• prime_numbers = [2, 3, 5]
• # copying a list
• numbers = prime_numbers.copy()
• print('Copied List:', numbers)
• cars = ['Ford', 'BMW', 'Volvo']

• cars.sort()
• print(cars)
• prime_numbers = [2, 3, 5, 7]
• # reverse the order of list elements
• prime_numbers.reverse()
• print('Reversed List:', prime_numbers)

• # Output: Reversed List: [7, 5, 3, 2]

You might also like