Python 6 (List and Tuples)
Python 6 (List and Tuples)
LIST
A list can be defined as a collection of values or items of different
types. The items in the list are separated with the comma (,) and
enclosed with the square brackets [].
Characteristics of Lists
The lists are ordered.
The element of the list can be accessed by index.
The lists are mutable types.
How to Create and Assign Lists
A list can be define as below
L1 = ["John", 102, "USA"]
L2 = [1, 2, 3, 4, 5, 6]
LIST
How to Access Values in Lists
Slicing works similar to strings; use the square bracket slice
operator ( [ ] ) along with the index or indices.
LIST
We can get the sub-list of the list using the following syntax.
list_varible(start:stop:step)
e.g. list = [1,2,3,4,5,6,7]
list[1:6:2]
Output: [2, 4, 6]
Unlike other languages, Python provides the flexibility to use the
negative indexing also. The negative indices are counted from
the right. The last element (rightmost) of the list has the index -1
LIST
list = [1,2,3,4,5]
print(list[-1]) // 5
print(list[-3:]) // [3, 4, 5]
print(list[:-1]) // [1, 2, 3, 4]
print(list[-3:-1]) // [3, 4]
Updating List values
Lists are the most versatile data structures in Python since they are mutable,
and their values can be updated by using the slice and assignment operator.
list = [1, 2, 3, 4, 5, 6]
list[2] = 10
print(list) // [1, 2, 10, 4, 5, 6]
# Adding multiple-element
list[1:3] = [89, 78]
print(list) // [1, 89, 78, 4, 5, 6]
LIST
How to Remove List Elements and Lists
To remove a list element, you can use either the del statement if you know exactly
which element(s) you are deleting or the remove() method if you do not know.
list=[1,'abc',6,7,8]
>>> del list[2]
>>> list // [1, 'abc', 7, 8]
>>> list.remove('abc')
>>> list // [1, 7, 8]
To remove an entire list, use the del statement:
del List
Pop()
List.pop()
Removes an element from a list.
This method differs from .remove() in two ways:
You specify the index of the item to remove, rather than the object itself.
The method returns a value: the item that was removed.
Python List Operator
list= [3,5,1,7,9,4,23]
Print(Sorted(list)) // [1, 3, 4, 5, 7, 9, 23]
Print(Sorted(list),reverse = True) // [23,9,7,5,4,3,1]
Sort()
sort() function is very similar to sorted() but unlike sorted it returns nothing and makes changes
to the original sequence. Moreover, sort() is a method of list class and can only be used with
lists.
Syntax: List_name.sort(key, reverse=False)
reverse: reverse=True will sort the list descending. Default is reverse=False
key: A function to specify the sorting criteria(s)
def myFunc(e):
return len(e)
x.sort(key=myFunc)
print(x)
Built in functions
Reversed():
for t in reversed(list):
print(t) // 23, 9,7,5,4,3,1
Reverse():
list.reverse()
list // [10, 9, 5, 4, 7, 2]
reverse() actually reverses the elements in the container. reversed()
doesn't actually reverse anything, it merely returns an object that can be
used to iterate over the container's elements in reverse order. If that's what
you need, it's often faster than actually reversing the elements.
Append:
list= [2,7,4,5,9]
list.append(10)
list // [2, 7, 4, 5, 9, 10]
Built in functions
.
TUPLE
a = [ [2, 4, 6, 8 ],
[ 1, 3, 5, 7 ],
[ 8, 6, 4, 2 ],
[ 7, 5, 3, 1 ] ]
for i in range(len(a)) :
for j in range(len(a[i])) :
print(a[i][j], end=" ")
print()
OUTPUT:
2468
1357
8642
7531
Lists Can Be Nested
You have seen that an element in a list can be any sort of object.
That includes another list. A list can contain sublists, which in
turn can contain sublists themselves, and so on to arbitrary
depth.
>>> x = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', ['hh', 'ii'], 'j']
Lists Can Be Nested
>>> x[1]
['bb', ['ccc', 'ddd'], 'ee', 'ff']
>>> x[1][0]
'bb'
>>> x[1][1]
['ccc', 'ddd']
>>> x[1][2]
'ee'
>>> x[1][3]
'ff‘
>>> print(x[1][1][0], x[1][1][1])
ccc ddd
List Comprehension in Python
List comprehensions are used for creating new lists from other
iterables.
As list comprehensions return lists, they consist of brackets
containing the expression, which is executed for each element
along with the for loop to iterate over each element.
This is the basic syntax:
new_list = [expression for_loop_one_or_more conditions]
For example, find the squares of a number using the for loop
numbers = [1, 2, 3, 4]
squares = []
for n in numbers:
squares.append(n**2)
print(squares)
List Comprehension in Python