Python for Lifetime — LIST
LIST in brief
Lists can store a sequence of values of different data types. Lists are iterable.
Declaration & initialization:
An empty list can be created using [] ,list() or list comprehension. Passing values within these as given in the above images will create list with values as shown in the above image.
List Operations :
‘+’ operator can be used to easily concatenate two lists.
All list values can be duplicated within the list using ‘*’ operator.
List Methods & Keywords:
When a function is associated with an object or a class, it is referred to as ‘method’.
Above image depicts the use of index(), count(), append(), extend() and insert() list methods.
index() — Gives the index of the item in the list.
count() — Gives the number of occurrences of the the item passed to the count() method.
append() — Appends the given item to the end of the list
extend() — Appends the given iterable item to the end of the list.
insert() — Insert the given element at the index mentioned.
append(new_value_append) vs extend(new_value_extend) — Although both of them appends values to the list, their behaviour is different.
append() will append new_value_append object to the list as it is , even if it is an iterable.
On the other hand, extend(new_value_append) will iterate through the given iterable object new_value_append and then append the elements one by one to the list.
reverse() — Reverses the order of list elements
sorted() — It will sort the list elements and returns a sorted list keeping the original list intact. By default the order is ‘ascending’ in nature. To achieve descending order sorting set reverse parameter as ‘ True’ . By default, reverse is ‘False’.
Example : sorted(myList2,reverse=True) — -> For descending order sort.
sort() — It will sort the list in ascending order by default. And the sorting changes will be reflected on the original list. A descending order sort can be achieved by setting reverse parameter to ‘True’.
Simply, assigning a list to another will make both the list variables refer to the same memory location. Any change to one list will reflect in the other.
In order to create a separate copy of the list, we use copy().
remove() — Removes the first occurrence from index 0 of the given value from the list.
pop() — pop() will remove the last element in the list and return that value.
pop(index) — It will pop the element at the index specified and return that value.
del list_obj[1] — will delete the element from the list when the list with index is specified.
del list_obj — It will remove and deallocate the list_obj. Any further reference will lead to “Name ‘list_obj’ not found Exception”.
clear() — It will clear all the list elements and make it an empty list.
pop() and remove() are methods and can delete one value at a time. del on the other hand, is a keyword and can remove multiple values at a time.
These are few ways to iterate through the list elements using for loop. Same could be achieved using while loop as well.
Coming up next : Python for Lifetime — Tuples
Thank you for your patience.