Advertisement
#Python - Creating a list with items
random_info =['Keep Smiling', 19, 78.59, 'A']
print(random_info)
['Keep Smiling', 19, 78.59, 'A']
#Python - Accessing each time of a list using its index.
misc =[19 ,-9, 9.99, 'Have a perfect day!']
print(misc)
print(misc[2])
print(misc[0])
print(misc[3])
print(misc[1])
[19, -9, 9.99, 'Have a perfect day!']
9.99
19
Have a perfect day!
-9
#Python - Iterating over the items in a list using for loop
# Creating a list
misc =[19 ,-9, 9.99, 'Have a perfect day!']
# Using for loop to iterate over items in a list
for val in misc:
print(val)
19
-9
9.99
Have a perfect day!
#Python - Once declared, a list is mutable
misc2 =[2 ,-19, 17.5, 'Make your life productive!']
print('misc : ', misc2)
#Trying to insert a new item at index 1 in a list
misc2[1]= 'Hey'
#Trying to insert a new item at index 2 in a list
misc2[2]= 5
print('Modified misc : ', misc2)
misc : [2, -19, 17.5, 'Make your life productive!']
Modified misc : [2, 'Hey', 5, 'Make your life productive!']
Methods | Description |
---|---|
index() | To find the index of an item in a list. |
count() | Finds the number of times an item exists in a list. |
insert() | Inserts an item at a specific index in a list. |
append() | Adds an item or a list to the end of a list. |
extend() | Extends a list to contain new items or another list. |
copy() | Returns copy of a list. |
clear() | Clears/removes all the items in a list. |
pop() | Removes an item from a specific index in a list. |
remove() | Removes a specific item in a list. |
sort() | Sorts all the items in a list in an ascending order. |
reverse() | Reverses the order of items in a list. |
Advertisement
#Python - Using the index() method of a list data type
misc =[-19, 2, 'think positive!', 2]
print('First index of item 2 : ', misc.index(2))
First index of item 2 : 1
#Python - Using the count() method with a list
misc =[5 , -9, 'After a sad day comes a glad day!', 5]
print('misc : ',misc)
print('Total number of times item 5 is contained in a list : ', misc.count(5))
misc : [5, -9, 'After a sad day comes a glad day!', 5]
Total number of times item 5 is contained in a list : 2
#Python - Using the len() function with a list
misc =[2019 , 9.999, 'Please, share your smile!']
print('misc : ', misc)
print('Total number of elements in list : ', len(misc))
misc : [2019, 9.999, 'Please, share your smile!']
Total number of elements in list : 2
#Python - Concatenating list using the arithmetic + operator
#First list
list1 =['A', -20]
#Second list
list2 =['have a good day', 2019]
#Printing lists
print('First List : ', list1)
print('Second List : ', list2)
#Creating a new list by concatenating existing lists
list3 = list1 + list2
#Printing new list
print('New List : ',list3)
First List : ['A', -20]
Second List : ['have a good day', 2019]
New List : ['A', -20, 'have a good day', 2019]
#Python - Using the remove() method of a list data type
misc =[-19, 2, 'think positive!', 2]
print('list : ', misc)
misc.remove(2)
print('updated list :',misc)
list : [-19, 2, 'think positive!', 2]
updated list : [-19, 'think positive!', 2]
#Python - Using pop() method of a list data type
misc =[-19, 2, 'think positive!', 2]
print('list : ', misc)
misc.pop(2)
print('updated list :',misc)
list : [-19, 2, 'think positive!', 2]
updated list : [-19, 2, 2]
#Python - Extending a list to contain another list using extend() method
#First list
list1 =['A', -20]
#Second list
list2 =['have a good day', 2019]
#Printing lists
print('First List : ', list1)
print('Second List : ', list2)
#Calling append() method to append list2 to the end of list1
list1.append(list2)
#Printing new list
print('Updating First List : ',list1)
#Calling append() method to append an object to the end of list1
list1.append(8.5)
#Printing new list
print('Updating First List : ',list1)
First List : ['A', -20]
Second List : ['have a good day', 2019]
Updating First List : ['A', -20, ['have a good day', 2019]]
Updating First List : ['A', -20, ['have a good day', 2019], 8.5]
#Python - Extending a list by calling the extend() method
#First list
list1 =['A', -20]
#Second list
list2 =['have a good day', 2019]
#Printing lists
print('First List : ', list1)
print('Second List : ', list2)
#Calling extend() method to extend list1 to the include a list2 i.e. iterable object
list1.extend(list2)
#Printing new list
print('Updating First List : ',list1)
#Calling extend() method to extend list1 to contain multiple items of a list i.e. iterable object.
list1.extend([29, 'haha'])
#Printing new list
print('Updating First List : ',list1)
#Calling extend() method to extend list1 to contain a tuple i.e. iterable object.
list1.extend((2.5, 1978))
#Printing new list
print('Updating First List : ',list1)
First List : ['A', -20]
Second List : ['have a good day', 2019]
Updating First List : ['A', -20, 'have a good day', 2019]
Updating First List : ['A', -20, 'have a good day', 2019, 29, 'haha']
Updating First List : ['A', -20, 'have a good day', 2019, 29, 'haha', 2.5, 1978]
Functions | Description |
---|---|
max() | Returns the maximum value in a list. |
min() | Returns the minimum value in a list. |
len() | Returs the total length of a list. |
sum() | Returns sum of all elements in a list. |
sorted() | Returns a sorted copy of a list. |
any() | Returns true if any element in a list is true, else false. |
all() | Returns true if all elements in a list are true, else false. |
#Python - Functions working with lists
#First list
list1 =[19, -20, 209, 799.99, -15.4]
#Second list
list2 =[1, True, False, 0] # 1 is boolean true, 0 for boolean false
# True(with capital T) is boolean true, False(with capital F) is boolean false
#Printing lists
print('List1 : ', list1)
print('List2 : ', list2)
#Using max() function with a list.
print('Maximum value in List1 : ', max(list1))
#Using min() function with a list.
print('Minimum value in List1 : ', min(list1))
#Using sum() function with a list.
print('Sum of values in List1 : ', sum(list1))
#Using sorted() function with a list.
print('Sorted copy of List1 : ', sorted(list1))
#Using all() function with a list.
print('all() boolean true values in List2 : ', all(list2))
#Using any() function with a list.
print('any boolean true value in List2 : ', any(list2))
List1 : [19, -20, 209, 799.99, -15.4]
List2 : [1, True, False, 0]
Maximum value in List1 : 799.99
Minimum value in List1 : -20
Sum of values in List1 : 992.59
Sorted copy of List1 : [-20, -15.4, 19, 209, 799.99]
all() boolean true values in List2 : False
any boolean true value in List2 : True
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement