Python List

Before going to original section first learn about python methods Enumerate():

Using Enumerate we can easily print index with value in python. This is python built-in method. Enumerate() method adds a counter to an iterable and returns it in a form of enumerate object.

# Python3 code to demonstrate 
# print list values with index
# using enumerate() 

# initializing list 
test_list = [10, 20, 30, 40, 50, 60] 

# print from default 0 index
print("print from default 0 index")
[print(count, value) for count, value in enumerate(test_list)]


# print from starting 100 index
print("print from custom starting 100 index")
[print(count, value) for count, value in enumerate(test_list, 100)]

Now check a value in list and finding indices of value examples

# Python3 code to demonstrate 
# finding indices of values 
# using enumerate() 

# initializing list 
test_list = [1, 3, 4, 3, 6, 7] 

print (f"Original list : {test_list}") 

# checking value 3 in the list or not 
print(f"3 in the list : {3 in test_list}")

# checking value 8 in the list or not 
print(f"8 in the list : {8 in test_list}")

# using count() 
# to check 3 elements occurs no of times in the list
print(f"No of occurence of 3 element in the list is : {test_list.count(3)}")

# using enumerate() 
# to find indices for 3 
res_list = [i for i, value in enumerate(test_list) if value == 3] 
		
# printing resultant list 
print (f"New indices list : {res_list}") 

Some useful link that I have followed:

Note: If you have any query or any upgradation of my writing or any mistakes please comment and suggest me. You are warmly welcomed always.