Python List Example- A Practical Implementation.

Python list is used to store multiple types of items or values. we can store any type of data structure inside a python list like simple values, tuples, or dictionaries, and moreover, we can store a list inside a list. here, I am going to explore Python List Example with practical implementation. Python List index starts with 0 and we can traverse the python list by positive index or negative index let us see some examples to understand this concept.

How to create a python list.

# A List of Integer values
li = [1,2,3,4,5,6,7,8,9]
print(li)

# A List of string types of values
li =["red","green","blue","yellow","pink]
print(li)

#A List of Mixed type of values
li=["green","red",1,2,3,4,5,6,12.2,12.5,True]
print(li)

# A List inside a List
li=[[1,2,3],[4,5,6],[7,8,9]]
print(li)

# A dictionary inside a List
li =[{"id":101,"name":"akash","age":22},{"id":102,"name":"vijay","age":23},{"id":103,"name":"rakesh","age":24}]
print(li)

How to Fetch python list Elements.

li =[{"id":101,"name":"akash","age":22},{"id":102,"name":"vijay","age":23},{"id":103,"name":"rakesh","age":24}]
print(li)

#Style-1 (Positive Indexes)
print(li[0])  # return {"id":101,"name":"akash","age":22}
print(li[1])  # return {"id":102,"name":"vijay","age":23}
print(li[3])  # return {"id":103,"name":"rakesh","age":24}

#Style-2 (negative Indexes)
print(li[-1])  # return {"id":103,"name":"rakesh","age":24}
print(li[-2])  # return {"id":102,"name":"vijay","age":23}
print(li[-2])  # return {"id":101,"name":"akash","age":22}

#Style-3  (Using for Loop)

for item in li:
   print(item)

Output:-
{"id":101,"name":"akash","age":22}
{"id":102,"name":"vijay","age":23}
{"id":103,"name":"rakesh","age":24}

Python List Example

Function that can be apply on python list .

1- Append: – This is a predefined function by which we can add elements at the last index of the list. let us understand this function through an example.

li=["red","blue","green","yellow","pink"]
print(li)
output:-["red","blue","green","yellow","pink"]

li.append("white)
print(li)
output:-["red","blue","green","yellow","pink","white"]

li.append("black)
print(li)
output:-["red","blue","green","yellow","pink","white","black"]

2- Extend: – This is a predefined function by which we can separate string characters and store every character at a separate index at the last of the list. let us understand the concept through an example.

li=["green","blue"]
print(li)
output:-["green","blue"]
li.extend("yellow")
print(li)
output:-["green","blue","y","e","l","l","o","w"]

You can see the impact of extend function on a list so here “yellow” word every character is getting a separate index inside a list as “w” index is -1 and “o” index is -2 and so on.

3- Pop: – Pop function is used to delete element from the list and it can be used in two ways 1st pop()- will remove values at the last of the list and 2nd pop(1) – will delete value at index 1. let us do a practical Implementation.

li=["red","blue","green","yellow","pink"]
li.pop()
print(li)
output:- ["red","blue","green","yellow"]

li.pop(0)
print(li)
output:- ["blue","green","yellow"]

4- remove: – remove function is also used to delete element from the list and but the working of the remove function is different. this function will not delete values based on the index so you need to give value instead. let us see one example.

li=["red","blue","green","yellow","pink"]
print(li)
li.remove("blue")
output:-["red,"green","yellow","pink"]

5- Count: – Count function is used to check how many times an element is present inside a list or check the occurrence of a particular element. let us understand it by an example.

li=["red","blue","green","yellow","pink","red","red"]
print(li.count("red"))
Output:-3

You can see in the above code “red” is present 3 times inside a List so output is 3.

Jashika Bhatt

Jashika is a professional blogger, self-published author and speaker at techbook and passionate about digital marketing.

Leave a Reply

Your email address will not be published. Required fields are marked *