Python - Lists
Python - Lists
Python Lists
Lists are ordered collection of data items.
They store multiple items in a single variable.
List items are separated by commas and enclosed within square brackets [].
Lists are changeable meaning we can alter them after creation.
Example 1:
lst1 = [1,2,2,3,5,4,6]
print(lst1)
print(lst2)
Output:
[1, 2, 2, 3, 5, 4, 6]
Example 2:
print(details)
Output:
As we can see, a single list can contain items of different data types.
List Index
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:
https://notebook.zoho.in/app/index.html#/notebooks/dcr5z062df283b6d34515b57bede07611926f/notecards/9wn9o143cd2a274104262931032864a665bbe 1/10
21/10/2024, 13:11 Python
Positive Indexing:
As we have seen that list items have index, as such we can access items using these indexes.
Example:
print(colors[2])
print(colors[4])
print(colors[0])
Output:
Blue
Green
Red
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:
print(colors[-1])
print(colors[-3])
print(colors[-5])
Output:
Green
Blue
https://notebook.zoho.in/app/index.html#/notebooks/dcr5z062df283b6d34515b57bede07611926f/notecards/9wn9o143cd2a274104262931032864a665bbe 2/10
21/10/2024, 13:11 Python
Red
if "Yellow" in colors:
print("Yellow is present.")
else:
print("Yellow is absent.")
Output:
Yellow is present.
if "Orange" in colors:
print("Orange is present.")
else:
print("Orange is absent.")
Output:
Orange is absent.
Range of Index:
You can print a range of list items by specifying where you want to start, where do you want to end and if
you want to skip elements in between the range.
Syntax:
animals = ["cat", "dog", "bat", "mouse", "pig", "horse", "donkey", "goat", "cow"]
https://notebook.zoho.in/app/index.html#/notebooks/dcr5z062df283b6d34515b57bede07611926f/notecards/9wn9o143cd2a274104262931032864a665bbe 3/10
21/10/2024, 13:11 Python
Output:
Here, we provide index of the element from where we want to start and the index of the element till which we
want to print the values.
Note: The element of the end index provided will not be included.
Example: printing all element from a given index till the end
animals = ["cat", "dog", "bat", "mouse", "pig", "horse", "donkey", "goat", "cow"]
Output:
When no end index is provided, the interpreter prints all the values till the end.
animals = ["cat", "dog", "bat", "mouse", "pig", "horse", "donkey", "goat", "cow"]
Output:
When no start index is provided, the interpreter prints all the values from start up to the end index provided.
animals = ["cat", "dog", "bat", "mouse", "pig", "horse", "donkey", "goat", "cow"]
https://notebook.zoho.in/app/index.html#/notebooks/dcr5z062df283b6d34515b57bede07611926f/notecards/9wn9o143cd2a274104262931032864a665bbe 4/10
21/10/2024, 13:11 Python
Output:
Here, we have not provided start and index, which means all the values will be considered. But as we have
provided a jump index of 2 only alternate values will be printed.
animals = ["cat", "dog", "bat", "mouse", "pig", "horse", "donkey", "goat", "cow"]
print(animals[1:8:3])
Output:
Here, jump index is 3. Hence it prints every 3rd element within given index.
List Comprehension
List comprehensions are used for creating new lists from other iterables like lists, tuples, dictionaries, sets,
and even in arrays and strings.
Syntax:
List = [Expression(item) for item in iterable if Condition]
Example 1: Accepts items with the small letter “o” in the new list
print(namesWith_O)
Output:
https://notebook.zoho.in/app/index.html#/notebooks/dcr5z062df283b6d34515b57bede07611926f/notecards/9wn9o143cd2a274104262931032864a665bbe 5/10
21/10/2024, 13:11 Python
print(namesWith_O)
Output:
List Methods
list.sort()
This method sorts the list in ascending order. The original list is updated
Example 1:
colors.sort()
print(colors)
num = [4,2,5,3,6,1,2,1,2,8,9,7]
num.sort()
print(num)
Output:
[1, 1, 2, 2, 2, 3, 4, 5, 6, 7, 8, 9]
Example:
colors.sort(reverse=True)
print(colors)
https://notebook.zoho.in/app/index.html#/notebooks/dcr5z062df283b6d34515b57bede07611926f/notecards/9wn9o143cd2a274104262931032864a665bbe 6/10
21/10/2024, 13:11 Python
num = [4,2,5,3,6,1,2,1,2,8,9,7]
num.sort(reverse=True)
print(num)
Output:
[9, 8, 7, 6, 5, 4, 3, 2, 2, 2, 1, 1]
reverse()
This method reverses the order of the list.
Example:
colors.reverse()
print(colors)
num = [4,2,5,3,6,1,2,1,2,8,9,7]
num.reverse()
print(num)
Output:
[7, 9, 8, 2, 1, 2, 1, 6, 3, 5, 2, 4]
index()
This method returns the index of the first occurrence of the list item.
Example:
print(colors.index("green"))
num = [4,2,5,3,6,1,2,1,3,2,8,9,7]
print(num.index(3))
https://notebook.zoho.in/app/index.html#/notebooks/dcr5z062df283b6d34515b57bede07611926f/notecards/9wn9o143cd2a274104262931032864a665bbe 7/10
21/10/2024, 13:11 Python
Output:
count()
Returns the count of the number of items with the given value.
Example:
print(colors.count("green"))
num = [4,2,5,3,6,1,2,1,3,2,8,9,7]
Output:
copy()
Returns copy of the list. This can be done to perform operations on the list without modifying the original
list.
Example:
newlist = colors.copy()
print(colors)
print(newlist)
Output:
append():
This method appends items to the end of the existing list.
https://notebook.zoho.in/app/index.html#/notebooks/dcr5z062df283b6d34515b57bede07611926f/notecards/9wn9o143cd2a274104262931032864a665bbe 8/10
21/10/2024, 13:11 Python
Example:
colors.append("green")
print(colors)
Output:
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:
print(colors)
Output:
extend():
This method adds an entire list or any other collection datatype (set, tuple, dictionary) to the existing list.
Example 1:
colors.extend(rainbow)
https://notebook.zoho.in/app/index.html#/notebooks/dcr5z062df283b6d34515b57bede07611926f/notecards/9wn9o143cd2a274104262931032864a665bbe 9/10
21/10/2024, 13:11 Python
print(colors)
Output:
Example:
print(colors + colors2)
Output:
https://notebook.zoho.in/app/index.html#/notebooks/dcr5z062df283b6d34515b57bede07611926f/notecards/9wn9o143cd2a274104262931032864a665bbe 10/10