Lists PDF
Lists PDF
Lists PDF
Lists
Combining lists with a string:
print(thislist)
Positive Indexing
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"]
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)
thislist.extend(tropical)
print(thislist)
Extend:
thislist.insert(1, "orange")
print(thislist)
Remove specified index – pop()
Remove specified item – Remove
print(thislist)
Remove specified item – Del
The del keyword also removes the specified index:
thislist.clear()
print(thislist)
Concatenation
List of Lists
“Sort” Function
Reverse
Index
Count()
numbers = [1,2]
if 3 in numbers:
print("Yes!")
else:
print("No!")