# Lists
mylist = ["apple", "banana", "cherry"]
print(mylist)
['apple', 'banana', 'cherry']
# 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
thislist = ["apple", "banana", "cherry"]
print(thislist)
['apple', 'banana', 'cherry']
# 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]
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
['apple', 'banana', 'cherry', 'apple', 'cherry']
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
#List items can be of any data type
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
print(list1, list2, list3)
print(list1)
print(list2)
print(list3)
['apple', 'banana', 'cherry'] [1, 5, 7, 9, 3] [True, False, False]
['apple', 'banana', 'cherry']
[1, 5, 7, 9, 3]
[True, False, False]
list1 = ["abc", 34, True, 40, "male"]
print(list1)
['abc', 34, True, 40, 'male']
mylist = ["apple", "banana", "cherry"]
mylist1= [9,92.97, 972,"haider", True, False]
print(type(mylist))
print(type(mylist1))
<class 'list'>
<class 'list'>
# The list() Constructor
# It is also possible to use the list() constructor when creating a
new list.
# Using the list() constructor to make a List:
thislist = list(("apple", "banana", "cherry")) # note the double
round-brackets
print(thislist)
['apple', 'banana', 'cherry']
# Access the list items
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
print(thislist[2])
print(thislist[0])
banana
cherry
apple
#Negative indexing
#Negative Indexing
#Negative indexing means start from the end
# -1 refers to the last item, -2 refers to the second last item
thislist = ["apple", "banana", "cherry"]
print(thislist[-1]) # it is going to print the last item of the list
cherry
# 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.
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon",
"mango"]
print(thislist[2:6])
# The search will start at index 2 (included) and end at index 6 (not
included).
['cherry', 'orange', 'kiwi', 'melon']
# By leaving out the start value, the range will start at the first
item:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon",
"mango"]
print(thislist[:4])
['apple', 'banana', 'cherry', 'orange']
#By leaving out the end value, the range will go on to the end of the
list
# This example returns the items from "cherry" to the end:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon",
"mango"]
print(thislist[2:])
['cherry', 'orange', 'kiwi', 'melon', 'mango']
#Range of Negative Indexes
# Specify negative indexes if you want to start the search from the
end of the list:
#This example returns the items from "orange" (-4) to, but NOT
including "mango" (-1):
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon",
"mango"]
print(thislist[-4:-1])
['orange', 'kiwi', 'melon']
#Check if Item Exists
# To determine if a specified item is present in a list use the in
keyword:
#Check if "apple" is present in the list:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
Yes, 'apple' is in the fruits list
#Change Item Value
#To change the value of a specific item, refer to the index number
# Change the second item in the list
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
['apple', 'blackcurrant', 'cherry']
# Change the range of item values in the list
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)
['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:
# Insert "watermelon" as the third item:
thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "watermelon")
print(thislist)
['apple', 'banana', 'watermelon', 'cherry']
#Python- Add items
#Append Items
# To add an item to the end of the list, use the append() method
#Using the append() method to append an item:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
['apple', 'banana', 'cherry', 'orange']
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
['apple', 'orange', 'banana', 'cherry']
#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)
['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']
#Add Any Iterable
#The extend() method does not have to append lists, you can add any
iterable object (tuples, sets, dictionaries etc.).
#Add elements of a tuple to a list:
thislist = ["apple", "banana", "cherry"]
thistuple = ("kiwi", "orange")
thislist.extend(thistuple)
print(thislist)
['apple', 'banana', 'cherry', 'kiwi', 'orange']
# Python - Remove List Items
#Remove Specified Item
#The remove() method removes the specified item.
#Remove "banana":
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
['apple', 'cherry']
#If there are more than one item with the specified value, the
remove() method removes the first occurrence
# Remove the first occurrence of "banana":
thislist = ["apple", "banana", "cherry", "banana", "kiwi"]
thislist.remove("banana")
print(thislist)
['apple', 'cherry', 'banana', 'kiwi']
#Remove Specified Index
# The pop() method removes the specified index.
#Remove the second item:
thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
print(thislist)
['apple', 'cherry']
#If you do not specify the index, the pop() method removes the last
item.
#Example: Remove the last item:
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
['apple', 'banana']
#Remove the first item:
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)
['banana', 'cherry']
#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.
#Clear the list content:
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
[]
#Loop Through a List
# You can loop through the list items by using a for loop
#Print all items in the list, one by one:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
apple
banana
cherry
#Loop Through the Index Numbers
#You can also loop through the list items by referring to their index
number.
#Use the range() and len() functions to create a suitable iterable.
#Print all items by referring to their index number:
thislist = ["apple", "banana", "cherry"]
for i in range(len(thislist)):
print(thislist[i])
apple
banana
cherry
#Using a While Loop
#You can loop through the list items by using a while loop.
#Use the len() function to determine the length of the list, then
start at 0 and loop your way through the list items by referring to
their indexes.
#Remember to increase the index by 1 after each iteration.
#Print all items, using a while loop to go through all the index
numbers
thislist = ["apple", "banana", "cherry"]
i = 0
while i < len(thislist):
print(thislist[i])
i = i + 1
apple
banana
cherry
#Sort List Alphanumerically
# List objects have a sort() method that will sort the list
alphanumerically, ascending, by default:
#Sort the list alphabetically:
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
['banana', 'kiwi', 'mango', 'orange', 'pineapple']
#Sort the list numerically:
thislist = [100, 50, 65, 82, 23]
thislist.sort()
print(thislist)
[23, 50, 65, 82, 100]
#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)
['pineapple', 'orange', 'mango', 'kiwi', 'banana']
#Sort the list descending:
thislist = [100, 50, 65, 82, 23]
thislist.sort(reverse = True)
print(thislist)
[100, 82, 65, 50, 23]
# Copy list Method
#Make a copy of a list with the copy() method:
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
['apple', 'banana', 'cherry']
#Use the list() method
#Another way to make a copy is to use the built-in method list().
#Example
#Make a copy of a list with the list() method:
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)
['apple', 'banana', 'cherry']
#Use the slice Operator
#You can also make a copy of a list by using the : (slice) operator.
#Example: Make a copy of a list with the : operator:
thislist = ["apple", "banana", "cherry"]
mylist = thislist[:]
print(mylist)
['apple', 'banana', 'cherry']
#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.
#Join two list:
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
['a', 'b', 'c', 1, 2, 3]
#Another way to join two lists is by appending all the items from
list2 into list1, one by one:
#Example: Append list2 into list1:
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
for x in list2:
list1.append(x)
print(list1)
['a', 'b', 'c', 1, 2, 3]
#you can use the extend() method, where the purpose is to add elements
from one list to another list:
#Example: Use the extend() method to add list2 at the end of list1:
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
['a', 'b', 'c', 1, 2, 3]