Python Lists by Vipin Bhai
Python Lists by Vipin Bhai
Example 2:
details = ["Abhijeet", 18, "FYBScIT", 9.8]
print(details)
Copy
Output: List Indexes
Each item/element in a list has its own unique index. This
index can be used to access any particular item from the
list. The first item has index [0], second item has index
[1], third item has index [2] and so on.
Example:
colors = ["Red", "Green", "Blue", "Yellow", "Green"]
# [0] [1] [2] [3] [4]
Copy
Positive Indexing:
As we have seen that list items have index, as such we
can access items using these indexes.
Example:
colors = ["Red", "Green", "Blue", "Yellow", "Green"]
# [0] [1] [2] [3] [4]
print(colors[2])
print(colors[4])
print(colors[0])
Copy
Output:
Blue
Green
Red
Copy
Negative Indexing:
Similar to positive indexing, negative indexing is also used
to access items, but from the end of the list. The last item
has index [-1], second last item has index [-2], third last
item has index [-3] and so on.
Example:
colors = ["Red", "Green", "Blue", "Yellow", "Green"]
# [-5] [-4] [-3] [-2] [-1]
print(colors[-1])
print(colors[-3])
print(colors[-5])
Copy
Output:
Green
Blue
Red
Copy
Range of Index:
You can print a range of list items by specifying where do
you want to start, where do you want to end and if you
want to skip elements in between the range.
Syntax:
List[start : end : jumpIndex]
Note: jump Index is optional. We will see this in given
examples.
insert():
This method inserts an item at the given index. User has
to specify index and the item to be inserted within the
insert() method.
Example:
colors = ["voilet", "indigo", "blue"]
# [0] [1] [2]
print(colors)
Copy
Output:
['voilet', 'green', 'indigo', 'blue']
Copy
extend():
This method adds an entire list or any other collection
datatype (set, tuple, dictionary) to the existing list.
Example 1:
#add a list to a list
colors = ["voilet", "indigo", "blue"]
rainbow = ["green", "yellow", "orange", "red"]
colors.extend(rainbow)
print(colors)
Copy
Output:
['voilet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red']
Copy
Example 2:
#add a tuple to a list
cars = ["Hyundai", "Tata", "Mahindra"]
cars2 = ("Mercedes", "Volkswagen", "BMW")
cars.extend(cars2)
print(cars)
Copy
Output:
['Hyundai', 'Tata', 'Mahindra', 'Mercedes', 'Volkswagen',
'BMW']
Copy
Example 3:
#add a set to a list
cars = ["Hyundai", "Tata", "Mahindra"]
cars2 = {"Mercedes", "Volkswagen", "BMW"}
cars.extend(cars2)
print(cars)
Copy
Output:
['Hyundai', 'Tata', 'Mahindra', 'Mercedes', 'BMW',
'Volkswagen']
Copy
Example 4:
#add a dictionary to a list
students = ["Sakshi", "Aaditya", "Ritika"]
students2 = {"Yash":18, "Devika":19, "Soham":19} #only
add keys, does not add values
students.extend(students2)
print(students)
Copy
Output:
['Sakshi', 'Aaditya', 'Ritika', 'Yash', 'Devika', 'Soham']
Copy
pop():
This method removes the last item of the list if no index
is provided. If an index is provided, then it removes item
at that specified index.
Example 1:
colors = ["Red", "Green", "Blue", "Yellow", "Green"]
colors.pop() #removes the last item of the list
print(colors)
Copy
Output:
['Red', 'Green', 'Blue', 'Yellow']
Copy
Example 2:
colors = ["Red", "Green", "Blue", "Yellow", "Green"]
colors.pop(1) #removes item at index 1
print(colors)
Copy
Output:
['Red', 'Blue', 'Yellow', 'Green']
Copy
remove():
This method removes specific item from the list.
Example:
colors = ["voilet", "indigo", "blue", "green", "yellow"]
colors.remove("blue")
print(colors)
Copy
Output:
['voilet', 'indigo', 'green', 'yellow']
Copy
del:
del is not a method, rather it is a keyword which deletes
item at specific from the list, or deletes the list entirely.
Example 1:
colors = ["voilet", "indigo", "blue", "green", "yellow"]
del colors[3]
print(colors)
Copy
Output:
['voilet', 'indigo', 'blue', 'yellow']
Copy
Example 2:
colors = ["voilet", "indigo", "blue", "green", "yellow"]
del colors
print(colors)
Copy
Output:
NameError: name 'colors' is not defined
Copy
We get an error because our entire list has been deleted
and there is no variable called colors which contains a
list.
clear():
This method clears all items in the list and prints an
empty list.
Example:
colors = ["voilet", "indigo", "blue", "green", "yellow"]
colors.clear()
print(colors)
Copy
Output:
[]
Change List Items
Changing items from list is easier, you just have to specify
the index where you want to replace the item with
existing item.
Example:
names = ["Harry", "Sarah", "Nadia", "Oleg", "Steve"]
names[2] = "Millie"
print(names)
Copy
Output:
['Harry', 'Sarah', 'Millie', 'Oleg', 'Steve']
Copy
Syntax:
List = [expression(item) for item in iterable if condition]
expression: it is the item which is being iterated.
iterable: it can be list, tuples, dictionaries, sets, and even
in arrays and strings.
condition: condition checks if the item should be added
to the new list or not.
num = [4,2,5,3,6,1,2,1,2,8,9,7]
num.sort()
print(num)
Copy
Output:
['blue', 'green', 'indigo', 'voilet']
[1, 1, 2, 2, 2, 3, 4, 5, 6, 7, 8, 9]
Copy
num = [4,2,5,3,6,1,2,1,2,8,9,7]
num.sort(reverse=True)
print(num)
Copy
Output:
['voilet', 'indigo', 'green', 'blue']
[9, 8, 7, 6, 5, 4, 3, 2, 2, 2, 1, 1]
Copy
num = [4,2,5,3,6,1,2,1,2,8,9,7]
num.reverse()
print(num)
Copy
Output:
['green', 'blue', 'indigo', 'voilet']
[7, 9, 8, 2, 1, 2, 1, 6, 3, 5, 2, 4]
Copy
num = [4,2,5,3,6,1,2,1,3,2,8,9,7]
print(num.index(3))
Copy
Output:
1
3
Copy
num = [4,2,5,3,6,1,2,1,3,2,8,9,7]
Copy
Output:
2
3
Copy