Python List
List: List is a collection which is ordered and changeable. Allows duplicate members. In Python lists are written with square brackets.
list = ["apple", "banana", "cherry"]
print(list)
Output: ['apple', 'banana', 'cherry']
- append(): Adds an element at the end of the list.
- extend(): Add all elements of a list to the another list.
- insert(): Insert an item at the defined index.
- remove(): Removes an item from the list.
- pop(): Removes and returns an element at the given index.
- clear(): Removes all items from the list.
- index(): Returns the index of the first matched item.
- len(): To determine how many items a list.
- negative indexing: It means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc.
- slicing: You can specify a range of indexes by specifying where to start and where to end the range. When specifying a range, the return value will be a new list with the specified items.
- del: The del keyword can also delete the list completely,
Example: Menu Driven Program to perform all the list operations
#Function Definition
def display_menu():
print("-------------------------------")
print('\t'"List Operations")
print("-------------------------------")
print("1. Add a New Element - append()")
print("2. Index")
print("3. Extend()")
print("4. Pop()")
print("5. Slicing")
print("6. Len()")
print("7. Negative Index")
print("8. Remove()")
print("9. Insert()")
print("10. Delete")
print("11. Clear()")
def list_operations():
a=["Apple","Banana","Cherry","Grapes","JackFruit","Pomegranate"]
while(1):
ch=int(input("Enter Your Choice: "))
if ch==1: #Append
print("`````````````````````````````````````````````````````````")
print('\t'"APPEND")
print('\t'"******")
print("Lists are",a)
item=input("Enter the Element to Append: ")
a.append(item)
print("Added Successfully:",a)
print("`````````````````````````````````````````````````````````")
elif ch==2: #Index
print("`````````````````````````````````````````````````````````")
print('\t'"INDEX")
print('\t'"*****")
print("Lists are",a)
i=int(input("Enter the Index to Find the Element: "))
print("The Element with Index",i,"is",a[i])
print("````````````````````````````````````````````````````````````````````````")
elif ch==3: #Extend
print("````````````````````````````````````````````````````````````````````````")
print('\t'"EXTEND")
print('\t'"******")
print("Elements A:",a)
b=["Orange"]
print("Elements B:",b)
a.extend(b)
print("Extend Elements are",a)
print("````````````````````````````````````````````````````````````````````````")
elif ch==4: #Pop
print("````````````````````````````````````````````````````````````````````````")
print('\t'"POP")
print('\t'"***")
print("Lists are",a)
a.pop()
print("Pop Elements are",a)
print("````````````````````````````````````````````````````````````````````````")
elif ch==5: #Slicing
print("````````````````````````````````````````````````````````````````````````")
print('\t'"SLICING")
print('\t'"*******")
print("Lists are",a)
print("Slicing an Element in lists",a[1:4])
print("````````````````````````````````````````````````````````````````````````")
elif ch==6: #Len
print("````````````````````````````````````````````````````````````````````````")
print('\t'"LENGTH")
print('\t'"******")
print("Lists are",a)
b=len(a)
print("Length of an Element is",b)
print("````````````````````````````````````````````````````````````````````````")
elif ch==7: #Negative Index
print("````````````````````````````````````````````````````````````````````````")
print('\t'"NEGATIVE INDEX")
print('\t'"**************")
print("Lists are",a)
i=int(input("Enter the Negative Index to Find the Element: "))
print("The Element with Negative Index",i,"is",a[i])
print("````````````````````````````````````````````````````````````````````````")
elif ch==8: #Remove
print("````````````````````````````````````````````````````````````````````````")
print('\t'"REMOVE")
print('\t'"******")
print("Lists are",a)
a.remove("Banana")
print("Remove Element",a)
print("````````````````````````````````````````````````````````````````````````")
elif ch==9: #Insert
print("````````````````````````````````````````````````````````````````````````")
print('\t'"INSERT")
print('\t'"******")
print("Lists are",a)
item=input("Enter the elements to be inserted: ")
a.insert(2,item)
print("The Elements are",a)
print("````````````````````````````````````````````````````````````````````````")
elif ch==10: #Delete
print("````````````````````````````````````````````````````````````````````````")
print('\t'"DELETE")
print('\t'"******")
print("Lists are",a)
del a[0]
print("The Elements are",a)
print("````````````````````````````````````````````````````````````````````````")
elif ch==11: #Clear
print("````````````````````````````````````````````````````````````````````````")
print('\t'"CLEAR")
print('\t'"*****")
print("Lists are",a)
a.clear()
print("The Elements are",a)
print("````````````````````````````````````````````````````````````````````````")
else:
print("````````````````````````````````````````````````````````````````````````")
print('\t'"INVALID OPERATIONS")
print("````````````````````````````````````````````````````````````````````````")
con=input("Do you wish to Continue Y/N: ")
if con=='N':
break
#Function Call
display_menu()
list_operations()
Output:
-------------------------------
List Operations
-------------------------------
1. Add a New Element - append()
2. Index
3. Extend()
4. Pop()
5. Slicing
6. Len()
7. Negative Index
8. Remove()
9. Insert()
10. Delete
11. Clear()
Enter Your Choice: 1
````````````````````````````````````````````````````````````````````````
APPEND
******
Lists are ['Apple', 'Banana', 'Cherry', 'Grapes', 'JackFruit', 'Pomegranate']
Enter the Element to Append: Mango
Added Successfully: ['Apple', 'Banana', 'Cherry', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango']
````````````````````````````````````````````````````````````````````````
Do you wish to Continue Y/N: Y
Enter Your Choice: 2
````````````````````````````````````````````````````````````````````````
INDEX
*****
Lists are ['Apple', 'Banana', 'Cherry', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango']
Enter the Index to Find the Element: 5
The Element with Index 5 is Pomegranate
````````````````````````````````````````````````````````````````````````
Do you wish to Continue Y/N: Y
Enter Your Choice: 3
````````````````````````````````````````````````````````````````````````
EXTEND
******
Elements A: ['Apple', 'Banana', 'Cherry', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango']
Elements B: ['Orange']
Extend Elements are ['Apple', 'Banana', 'Cherry', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango', 'Orange']
````````````````````````````````````````````````````````````````````````
Do you wish to Continue Y/N: Y
Enter Your Choice: 4
````````````````````````````````````````````````````````````````````````
POP
***
Lists are ['Apple', 'Banana', 'Cherry', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango', 'Orange']
Pop Elements are ['Apple', 'Banana', 'Cherry', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango']
````````````````````````````````````````````````````````````````````````
Do you wish to Continue Y/N: Y
Enter Your Choice: 5
````````````````````````````````````````````````````````````````````````
SLICING
*******
Lists are ['Apple', 'Banana', 'Cherry', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango']
Slicing an Element in lists ['Banana', 'Cherry', 'Grapes']
````````````````````````````````````````````````````````````````````````
Do you wish to Continue Y/N: Y
Enter Your Choice: 6
````````````````````````````````````````````````````````````````````````
LENGTH
******
Lists are ['Apple', 'Banana', 'Cherry', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango']
Length of an Element is 7
````````````````````````````````````````````````````````````````````````
Do you wish to Continue Y/N: Y
Enter Your Choice: 7
````````````````````````````````````````````````````````````````````````
NEGATIVE INDEX
**************
Lists are ['Apple', 'Banana', 'Cherry', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango']
Enter the Negative Index to Find the Element: -4
The Element with Negative Index -4 is Grapes
````````````````````````````````````````````````````````````````````````
Do you wish to Continue Y/N: Y
Enter Your Choice: 8
````````````````````````````````````````````````````````````````````````
REMOVE
******
Lists are ['Apple', 'Banana', 'Cherry', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango']
Remove Element ['Apple', 'Cherry', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango']
````````````````````````````````````````````````````````````````````````
Do you wish to Continue Y/N: Y
Enter Your Choice: 9
````````````````````````````````````````````````````````````````````````
INSERT
******
Lists are ['Apple', 'Cherry', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango']
Enter the elements to be inserted: Lemon
The Elements are ['Apple', 'Cherry', 'Lemon', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango']
````````````````````````````````````````````````````````````````````````
Do you wish to Continue Y/N: Y
Enter Your Choice: 10
````````````````````````````````````````````````````````````````````````
DELETE
******
Lists are ['Apple', 'Cherry', 'Lemon', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango']
The Elements are ['Cherry', 'Lemon', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango']
````````````````````````````````````````````````````````````````````````
Do you wish to Continue Y/N: Y
Enter Your Choice: 11
````````````````````````````````````````````````````````````````````````
CLEAR
*****
Lists are ['Cherry', 'Lemon', 'Grapes', 'JackFruit', 'Pomegranate', 'Mango']
The Elements are []
````````````````````````````````````````````````````````````````````````
Do you wish to Continue Y/N: Y
Enter Your Choice: 13
````````````````````````````````````````````````````````````````````````
INVALID OPERATIONS
````````````````````````````````````````````````````````````````````````
Do you wish to Continue Y/N: N
If you have any doubts, feel free to post it in comment Section.
Comments