0% found this document useful (0 votes)
32 views4 pages

Python List Operations Guide

The document discusses various methods to manipulate and work with lists in Python. It covers creating, accessing, modifying lists using methods like append, insert, remove, pop. It also discusses sorting, copying and joining lists.

Uploaded by

Avishi Oj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

Python List Operations Guide

The document discusses various methods to manipulate and work with lists in Python. It covers creating, accessing, modifying lists using methods like append, insert, remove, pop. It also discusses sorting, copying and joining lists.

Uploaded by

Avishi Oj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

File: /home/devendra/work/Python-2022/Programs/29_lists.

txt Page 1 of 4

##thislist = ["apple", "banana", "cherry"]


##print(thislist)

## Lists allow duplicate values:

##thislist = ["apple", "banana", "cherry", "apple", "cherry"]


##print(thislist)

## Print the number of items in the list:

##print(len(thislist))

## List items can be of any data type:

##list1 = ["apple", "banana", "cherry"]


##list2 = [1, 5, 7, 9, 3]
##list3 = [True, False, False]

## A list can contain different data types:

##list1 = ["abc", 34, True, 40, "male"]

## What is the data type of a list?

##print(type(list1))

## The list() Constructor

##thislist = list(("apple", 1.2 , 1, True, "banana", "cherry")) # note the double


round-brackets
##print(thislist)

## Access List Items

##thislist = ["apple", "banana", "cherry", 2, 5, 23]


##print(thislist[1])
##print(thislist[-1])
##print(thislist[2:5])
##print(thislist[:4])
##print(thislist[2:])
##print(thislist[-3:-1])

## Check if Item Exists

##thislist = ["apple", "banana", "cherry"]


##if "apple" in thislist:
## print("Yes, 'apple' is in the fruits list")

## Change Item Value

##thislist = ["apple", "banana", "cherry"]


##thislist[1] = "blackcurrant"
##print(thislist)
##
##thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
##thislist[1:3] = ["blackcurrant", "watermelon"]
##print(thislist)
##
##thislist = ["apple", "banana", "cherry"]
##thislist[1:2] = ["blackcurrant", "watermelon"]
##print(thislist)

## The insert() method inserts an item at the specified index:

##thislist = ["apple", "banana", "cherry"]


File: /home/devendra/work/Python-2022/Programs/29_lists.txt Page 2 of 4

##thislist.insert(2, "watermelon")
##print(thislist)

## To add an item to the end of the list, use the append() method:

##thislist = ["apple", "banana", "cherry"]


##thislist.append("orange")
##print(thislist)

## To append elements from another list to the current list,


##use the extend() method.

##thislist = ["apple", "banana", "cherry"]


##tropical = ["mango", "pineapple", "papaya"]
##thislist.extend(tropical)
##print(thislist)

##thislist = ["apple", "banana", "cherry"]


##thistuple = ("kiwi", "orange")
##thislist.extend(thistuple)
##print(thislist)

## The remove() method removes the specified item.

##thislist = ["apple", "banana", "cherry"]


##thislist.remove("banana")
##print(thislist)

## Remove the first occurance of "banana":

##thislist = ["apple", "banana", "cherry", "banana", "kiwi"]


##thislist.remove("banana")
##print(thislist)

## The pop() method removes the specified index.

##thislist = ["apple", "banana", "cherry"]


##thislist.pop(1)
##print(thislist)

## Remove the last item:

##thislist = ["apple", "banana", "cherry"]


##thislist.pop()
##print(thislist)

## The del keyword also removes the specified index:

##thislist = ["apple", "banana", "cherry"]


##del thislist[0]
##print(thislist)

## The del keyword can also delete the list completely.

##thislist = ["apple", "banana", "cherry"]


##del thislist
##print(thislist)

## The clear() method empties the list.

##thislist = ["apple", "banana", "cherry"]


##thislist.clear()
##print(thislist)

## You can loop through the list items by using a for loop:
File: /home/devendra/work/Python-2022/Programs/29_lists.txt Page 3 of 4

##thislist = ["apple", "banana", "cherry"]


##for x in thislist:
## print(x)

##list = [34, 2, 7, 4, 9 , 21, 32, 45]


##for i in list:
## print(i)

## Print all items by referring to their index number:

##thislist = ["apple", "banana", "cherry"]


##for i in range(len(thislist)):
## print(thislist[i])

## Using a While Loop

##thislist = ["apple", "banana", "cherry"]


##i = 0
##while i < len(thislist):
## print(thislist[i])
## i = i + 1

## A short hand for loop that will print all items in a list:

##thislist = ["apple", "banana", "cherry"]


##[print(x) for x in thislist]

## Sort List Alphanumerically

##thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]


##thislist.sort()
##print(thislist)

##
##thislist = [100, 50, 65, 82, 23]
##thislist.sort()
##print(thislist)
##
##thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
##thislist.sort(reverse = True)
##print(thislist)
##
##thislist = [100, 50, 65, 82, 23]
##thislist.sort(reverse = True)
##print(thislist)

## By default the sort() method is case sensitive, resulting in


## all capital letters being sorted before lower case letters:

##thislist = ["banana", "Orange", "Kiwi", "cherry"]


##thislist.sort()
##print(thislist)

## Copy a List
## You cannot copy a list simply by typing list2 = list1

##thislist = ["apple", "banana", "cherry"]


##mylist = thislist.copy()
##print(mylist)

##
##thislist = ["apple", "banana", "cherry"]
##mylist = list(thislist)
##print(mylist)
File: /home/devendra/work/Python-2022/Programs/29_lists.txt Page 4 of 4

## Join Two Lists

##list1 = ["a", "b", "c"]


##list2 = [1, 2, 3]
##
##list3 = list1 + list2
##print(list3)

## Or you can use the extend() method,


##where the purpose is to add elements from one list to another list:
######

##list1 = ["a", "b" , "c"]


##list2 = [1, 2, 3]
##
##for x in list2:
## list1.append(x)
##
##print(list1)

You might also like