0% found this document useful (0 votes)
7 views

Python List Exercises and Examples

The document provides a comprehensive guide to various Python list operations, including creating lists, accessing elements, modifying lists, and performing common tasks such as sorting, reversing, and merging. It includes examples for each operation, demonstrating how to manipulate lists with different data types and methods. Additionally, it covers advanced topics like nested lists and checking for item existence.

Uploaded by

mohaalhabshi515
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python List Exercises and Examples

The document provides a comprehensive guide to various Python list operations, including creating lists, accessing elements, modifying lists, and performing common tasks such as sorting, reversing, and merging. It includes examples for each operation, demonstrating how to manipulate lists with different data types and methods. Additionally, it covers advanced topics like nested lists and checking for item existence.

Uploaded by

mohaalhabshi515
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Python List Exercises and Examples

1. Python list of strings


groceryList = ["Rice","Wheat","Barley"]
print(groceryList)

2. Python list of numbers


myNumbers = [1,5,9,16,28]
print(myNumbers)

3. Python list with duplicate values


myNumbers = [1,5,5,9,9,9,16,28]
print(myNumbers)

4. Python list that contains different data type values


hetList = ["Python",123,24.5,True,35,"a"]
print(hetList)

5. Finding the length of a list in Python


PList = ["CSharp","Python","PHP","R"]
print(len(PList))
6. Finding the index of items in a Python list
fruits = ["apple","banana","mango"]
bananaIndex = fruits.index("banana")
mangoIndex = fruits.index("mango")
print(bananaIndex)
print(mangoIndex)

pythonList = ["p","y","t","h","o","n"]

# to get the 1st item


print(pythonList[0])

#to get the 4th item


print(pythonList[3])

# Negative indexing to get last item


print(pythonList[-1])

# To get second last item


print(pythonList[-2])

7. Slicing of lists in Python


myList = ['p','y','t','h','o','n','l','i','s','t']

# elements from 5 to 8
print(myList[4:8])

# from 1st element to 4th element


print(myList[:4])

# from 8th element to last element


print(myList[7:])

# from 1st to last elements


print(myList[:])

# negative slicing
print(myList[-1:-9])

print(myList[-5:])

print(myList[:-4])

8. Adding items to a list using insert() and append() in Python


If we use the append method to add an item to a list, the new item will
be added at the end of the list.

numbers = [10,20,30,40,50,60]

numbers.append(70)
print(numbers)

If we use the insert method to add items to a list, we have to specify the
position at which we want to insert an item. The insert() method takes
two arguments [ insert(position,item) ].
numbers = [10,20,30,40,50,60]

numbers.insert(0,5)
print(numbers)

9. Merging two lists using extend method in Python


list1 = ["Apple","Orange","Mango"]
list2 = ["Grape","Strawberry"]

list1.extend(list2)
print(list1)
10. Removing items from a list using pop() in Python
x = [23,45,67,89,34,12]

# removing the last element


x.pop()
print(x)

# removing the 0th index element


x.pop(0)
print(x)

11. Removing items from a list using remove() in Python

dryFruits = ["Cashew nuts","Dates","Almonds","Raisins"]

dryFruits.remove("Raisins")
print(dryFruits)

12. Remove all the elements from a list in Python

dryFruits = ["Cashew nuts","Dates","Almonds","Raisins"]


dryFruits.clear()
print(dryFruits)

13. Creating a copy of a list in Python

dryFruits = ["Cashew nuts","Dates","Almonds","Raisins"]

d = dryFruits.copy()
print(d)

14. Finding the number of times an item is present in a list


myList = [1,2,2,3,3,3,4,4,4,4]

count = myList.count(4)
print(count)

15. Reversing a list in Python


myList = ["Pen","Pencil","Book"]

myList.reverse()
print(myList)
16. Sorting a list in Python
myList = ["Pen","Pencil","Book","Eraser"]

myList.sort()
print(myList)

17. Deleting items in a list using del keyword


colorList = ["Red","Blue","Green","Yellow","Orange","Purple"]

# deleting an item
del colorList[2]
print(colorList)

# deleting multiple items


del colorList[1:3]
print(colorList)

# deleting the entire list


del colorList

18. Nested list in Python


nestedList = ["Apple",["Ball",1,2.5],['a',128]]
print(nestedList)
print(nestedList[0])
print(nestedList[1])
print(nestedList[2])

19. Creating a list using the list() constructor


myList = list(("Mac","Windows","Linux"))
print(myList)

20. Checking whether an item exists in a list or not


myList = list(("Mac","Windows","Linux"))

if "Mac" in myList:
print("Yes")
else:
print("No")

if "Linux" not in myList:


print("Yes")
else:
print("No")
21. Concatenating lists in Python
myList1 = ["Mac","Windows","Linux"]
myList2 = ["Book","Pencil","Pen"]

combinedList = myList1 + myList2

print(combinedList)

22. Repeating a list in Python


myList1 = ["Mac","Windows"]

combinedList = myList1 * 3
print(combinedList)

You might also like