Lists PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Python

Lists
Combining lists with a string:

names = [“Paul”, “Phillip”, “Paula”,


“Phillipa”]
ages = [12,15,11,14]
print(names[1],"is",ages[1])
Len:

names = ["John", "David", "Clare"]


print(len(names))
Change item value

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


thislist[1] = "blackcurrant"

print(thislist)
Positive Indexing

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


print(thislist[1])

Negative Indexing
thislist = ["apple", "banana", "cherry"]
print(thislist[-2])
Printing specific items = Slicing
fruit =
["banana","lemon","orange","kiwi","
cherry","mango"]

print(fruit[1:4])
Range of indexes = Slicing

thislist =
["apple", "banana", "cherry", "orange",
"kiwi", "melon", "mango"]

print(thislist[2:5])
Range of indexes = Slicing
thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon", "
mango"]
print(thislist[:4])

thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon", "
mango"]
print(thislist[2:])
Range of negative indexes = Slicing

thislist =
["apple", "banana", "cherry", "orange", "kiwi",
"melon", "mango"]

print(thislist[-4:-1])
Change a range of item values = Slicing

thislist =
["apple", "banana", "cherry", "orange", "kiwi",
"mango"]

thislist[1:3] = ["blackcurrant", "watermelon"]


print(thislist)
If you insert more items than you replace, the
new items will be inserted where you specified,
and the remaining items will move accordingly:

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


thislist[1:2] = "blackcurrant", "watermelon"
print(thislist)
If you insert less items than you replace, the new
items will be inserted where you specified, and
the remaining items will move accordingly:

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


thislist[1:3] = "watermelon"
print(thislist)
 Change the second and third value by replacing it with one value
Slicing – Summary
Append items

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


thislist.append("orange")

print(thislist)
 To add an item to the end of the list, use the append() method.
Append:

 numbers = [3,5,9,6]
 numbers.append(7)
 print(numbers)

 names = [“Rita”, “Sue”]


 names.append(“Bob”)
 print(names)
Extend list

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


tropical = ["mango", "pineapple", "papaya"]

thislist.extend(tropical)

print(thislist)
Extend:

names = [“Michael”, “Carl”, “Mary”]


extraNames = ["Terry”, “John”,
“Michael”]
names.extend(extraNames)
print(names)
Insert items

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


thislist.insert(2, "watermelon")
print(thislist)
 Insert "watermelon" as the third item.
Insert items

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

thislist.insert(1, "orange")

print(thislist)
Remove specified index – pop()
Remove specified item – Remove

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


thislist.remove("banana")

print(thislist)
Remove specified item – Del
The del keyword also removes the specified index:

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


del thislist[0]
print(thislist)

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


del thislist
Clear the list

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

thislist.clear()

print(thislist)
Concatenation
List of Lists
“Sort” Function
Reverse
Index
Count()

Count() will return the number of occurrences of an element in


a list.
Min & Max
Lists + string + IF …else

 numbers = [1,2]
if 3 in numbers:
print("Yes!")
else:
print("No!")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy