Python List THEORY
Python List THEORY
A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements.
Each element or value that is inside of a list is called an item. Just as strings are defined as characters
between quotes, lists are defined by having values between square brackets [ ]
In the first line, we use square brackets to create a list that contains four elements and then assign it
to the dog_breeds variable. In the second line, the list is printed through the variable's name. All the
elements are printed in the same order as they were stored in the list because lists are ordered.
numbers = [1, 2, 3, 4, 5]
print(numbers) # [1, 2, 3, 4, 5]
2. Slicing : get the sublist (subsequence from the sequential quantity) You can return a range of items
by using the
slice syntax.
d = st[0] d = st[0:3]
L=[]
#integer
num = [10,20,30,40,50]
print()
print()
#list functions
a = ["red","white","blue"]
b = [3,4,5,6,6]
c=a+b
print(c)
print()
del a[1]
print(a)
print()
#insert in list
print(a)
print()
#append in list
b.append(333)
print(b)
print()
#index in list
print(x)
print()
a.reverse()
print(a)
print()
#count
x = b.count(6)
print(x)
x = [10,20,20.5,10.5,2.55,"ganesh","vishnu"]
print("total list=",x)
print()
List Comprehensions
List comprehensions provide a concise way to create lists. List comprehension is a complete substitute
for the lambda function as well as the functions map(), filter() and reduce()
The list comprehension starts with a '[' and ']', to help you remember that the
expression