Python Lists

A list is a collection which is ordered and changeable. In Python lists are written with square brackets. It is mutable.

Create List

print('A list is a data structure in Python that is a mutable')
my_string_list = ['my','string','list']
print(f'This list has {len(my_string_list)} elements\nElements:\t{my_string_list}')

#number list - including integer, float, and complex data
my_number_list = [1,2,3.14j,4.0]
print(f'This list has {len(my_number_list)} elements\nElements:\t{my_number_list}')
# mixed datatypes

my_mixed_list = ['my','string','list',1,2,3.14j,4.0]
print(f'This list has {len(my_mixed_list)} elements\nElements:\t{my_mixed_list}')

Lists Indexing

# using 2 examples list1 and list2 and showing indexing example

list1 = ['string1', 'string2', 0001, 2020, 2.0]
list2 = [1, 2, 3, 4, 5, 6, 7 ]
print (‘list1[0]:’, list1[0])
# providing starting and end point index in list
print ("list2[1:5]: ", list2[1:5])

output; list1[0]: string1
output; list1[0]: [2, 3, 4, 5]

Negative Indexing

Negative Indexing means that the index (value = -1) gives the last element.
The negative indexing starts from where the array ends.
# starting from end of the string
x = "Hello, World!"
print(x[-6:])
# output; World!
# starting from second last point from the end of the string 
x = "Hello, World!"
print(x[-6:-1])
# output; World
# starting from Third last point from the end of the string 
x = "Hello, World!"
print(x[-6:-2])
# output; Worl

Lists – Access Items

Accessing list items(elements) one-by-one
my_mixed_list = ['my','string','list',1,2,3.14j,4.0]
print(f'This list has {len(my_mixed_list)} elements\nElements: {my_mixed_list}\n')
for index in range (len(my_mixed_list)):
    print(f'Accessing item number #{index + 1}, Value = {my_mixed_list[index]}, \tand Type =         {type(my_mixed_list[index])}')

Lists Operations – max(),min() methods

my_string_list, my_number_list = ['elephant', 'zebra', 'ant'], [456, 700, 200]
print(f'List = {my_string_list}\nMax value = {max(my_string_list)} \nMin value = {min(my_string_list)}')
print(f'List = {my_number_list}\nMax value = {max(my_number_list)} \nMin value = {min(my_number_list)}')

List Operations – sort() method

numbers= [10, 5, 17]

cars.sort()

#output- [5, 10, 17]


cars = ['Ford', 'BMW', 'Volvo']

cars.sort()

#output- [BMW, Ford, Volvo]

List Operations – append, Remove, extend, reverse

remove() method

list_of_students = ['old student 1', 'old student 2', 'old student 3', 'old student 4', 'new student 1', 'new student 2']
print(f'list of students\n{list_of_students}')
print('New student 1 left, so removed from the list') # remove entry
list_of_students.remove('new student 1')# removing new student 1
print(f'updated list of students\n{list_of_students}')
print('New student 2 also left, so removed from the list') # remove entry
list_of_students.remove('new student 2')# removing new student 2
print(f'updated list of students\n{list_of_students}')

append() method

# list Append - adding items to list
list1= ['abc', 'def', 'ghi']
list1.append('jkl')
print (list1)
#output: ['abc', 'def', 'ghi', 'jkl']

# list Append - adding items to list
list1= ['abc', 'def', 'ghi']
list2= ['jkl', 'mno', 'pqr']
list1.append(list2)
print (list1)
#output: ['abc', 'def', 'ghi', ['jkl', 'mno', 'pqr']]

extend() method

#List Operations - extends

old_list_of_students = ['old student 1', 'old student 2', 'old student 3', 'old student 4']
new_list_of_students = ['new student 1', 'new student 2']
print('Now instead of appending single single element\nWe will append a whole new list i.e: extends our list\n')
print(f'List 1: {old_list_of_students}\nList 2: {new_list_of_students}')

# new list added into the old one

old_list_of_students.extend(new_list_of_students)
print(f'renewed the old Extended list:\n{old_list_of_students}')

reverse() method

#List Operations - reverse

normal_order_list = ['alpha', 'beta', 'gamma', 'delta','pi']
print(f'Normal order list:\n{normal_order_list}\n')
normal_order_list.reverse() 

# changing the order...Reversing the order

print(f'Reversed order list:\n{normal_order_list}')

Python Lists- Delete element from list

# initializing list  
lis = [2, 1, 3, 5, 4, 3, 8] 
print("printing list before deleting: ",lis)
# now deleting 3rd element from the list lis 
del lis[2]
print("printing list after deleting: ",lis)

#output: printing list before deleting:  [2, 1, 3, 5, 4, 3, 8]
#output: printing list after deleting:  [2, 1, 5, 4, 3, 8]