regularpython@gmail.com
Lists
The most important data type in python is a list. The list stores the sequence of data. The list is mutable means we can modify, update, insert and delete the data. Each element in the list is assigned a number of its position or index. The first index is zero, the second is one and so forth.
Examples:
Fruits = [“oranges”, “grapes”, “bananas”, “mangoes”] Numbers = [1,2,3,4,5,6] Student = [{“name”:”sai”, “marks”:513}, {“name”:”ram”, “marks”:500}] Movies = [(‘guru’, ‘telugu’), (‘Aaru’, ‘tamil’)] We can also take all data types in a list. Data = [“oranges”, 1,2, {“name”:”sai”, “marks”:513}, (‘guru’, ‘telugu’),[1,2,3]]
How to access values from List?
fruits = ["oranges", "grapes", "bananas", "mangoes"]
print(fruits[0])
output: oranges.
Example2:
fruits = ["oranges", "grapes", "bananas", "mangoes"]
print(fruits[1:3])
output: ['grapes', 'bananas']
Example3:
fruits = ["oranges", "grapes", "bananas", "mangoes"]
print(fruits[-1])
output: mangoes
How to update list?
fruits = ["oranges", "grapes", "bananas", "mangoes"]
print("Before updating", fruits[1])
print(fruits)
print('----------------------------------------')
fruits[1] = 1
print('After updating', fruits[1])
print(fruits)
How to delete element from list?
fruits = ["oranges", "grapes", "bananas", "mangoes"]
print("Before deleting", fruits)
print('----------------------------------------')
del fruits[1]
print('After deleting', fruits)
output:
Before deleting ['oranges', 'grapes', 'bananas', 'mangoes']
----------------------------------------
After deleting ['oranges', 'bananas', 'mangoes']
List operations
How to find the length of a list?
There is a length function to find the length of a list and this function is most important to find data types length.
fruits = ["oranges", "grapes", "bananas", "mangoes"]
print("Length of a list", len(fruits))
output:
Length of a list 4
How to concatenate lists in python?
To concatenate lists in python, we can use + operator.
fruits = ["oranges", "grapes", "bananas", "mangoes"]
vegitables = ["tomato", "onions", "carrots", "potato"]
print(fruits + vegitables)
output:
['oranges', 'grapes', 'bananas', 'mangoes', 'tomato', 'onions', 'carrots', 'potato']
How to repeat list in twice or more?
fruits = ["oranges", "grapes", "bananas", "mangoes"] * 2
print(fruits)
output:
['oranges', 'grapes', 'bananas', 'mangoes', 'oranges', 'grapes', 'bananas', 'mangoes']
What is the search operator in lists?
'In' is a search operator in the list. If you want to search an element in a list you can use it 'in operator'.
fruits = ["oranges", "grapes", "bananas", "mangoes"]
if "grapes" in fruits:
print('Fruit found')
else:
print("Fruit not found")
output:
Fruit found
How to print lists element by using loops?
Using for loop:
fruits = ["oranges", "grapes", "bananas", "mangoes"]
for fruit in fruits:
print(fruit)
output:
oranges
grapes
bananas
mangoes
using while loop:
fruits = ["oranges", "grapes", "bananas", "mangoes"]
while fruits:
print(fruits.pop())
output:
mangoes
bananas
grapes
oranges
Built-in functions
Function |
Description |
fruits.append(obj) |
It is used to append objects to list. |
fruits.count(obj) |
It is used to count how many times an item occurs in a given list. |
fruits.extend(seq) |
It is used to append a list to another list. |
fruits.index(obj) |
It is used to find the index value of an element. |
fruits.insert(obj) |
It is used to insert an element in a particular position. |
fruits.pop(obj) |
It is used to remove an element from list and returns removed element. |
fruits.remove(obj) |
It is used to remove a particular element. |
fruits.reverse() |
It is used to reverse the list. There is a short cut to reverse the list Ex: fruits = [“oranges”, “grapes”, “bananas”, “mangoes”] Print(fruits[::-1]) |
fruits.sort() |
It is used to sort the list. |