[go: up one dir, main page]

0% found this document useful (0 votes)
87 views3 pages

List Comprehension:: A ("Apple", "Banana", "Cherry") For I in Range (Len (A) ) : Print (A (I) )

This document discusses various methods for manipulating lists in Python, including: 1. Looping through lists using for loops and indexing, as well as while loops. 2. List comprehensions for creating new lists from existing lists based on conditions. 3. Sorting lists in ascending and descending order using the sort() method. 4. Customizing the sort order using key functions. 5. Examples of combining, copying, and modifying lists through methods like append(), extend(), and + operators. 6. Problems involving swapping the first and last elements of a list and checking if a number is a "perfect number".

Uploaded by

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

List Comprehension:: A ("Apple", "Banana", "Cherry") For I in Range (Len (A) ) : Print (A (I) )

This document discusses various methods for manipulating lists in Python, including: 1. Looping through lists using for loops and indexing, as well as while loops. 2. List comprehensions for creating new lists from existing lists based on conditions. 3. Sorting lists in ascending and descending order using the sort() method. 4. Customizing the sort order using key functions. 5. Examples of combining, copying, and modifying lists through methods like append(), extend(), and + operators. 6. Problems involving swapping the first and last elements of a list and checking if a number is a "perfect number".

Uploaded by

Yamini Koneti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Loop through a list: fruits = ["apple", "banana", "cherry",

a= ["apple", "banana", "cherry"] "kiwi", "mango"]


for i in range(len(a)):
print(a[i]) newlist = [x if x != "banana" else
output: apple "orange" for x in fruits]
banana print(newlist)
cherry o/p:
using index numbers: ['apple', 'orange', 'cherry', 'kiwi',
'mango']
a = ["apple", "banana", "cherry"] Sorting off lists:
for i in range(len(a)): Sort () lists alphabetically in
print(a[i]) ascending order by default.
using a while loop: ----------------------------------
a = ["orange", "mango", "kiwi",
a = ["apple", "banana", "cherry"] "pineapple", "banana"]
i = 0 a.sort()
while i < len(a): print(a)
print(a[i]) output:
i = i + 1 ['banana', 'kiwi', 'mango', 'orange',
List Comprehension: 'pineapple']
----------------------------------------
fruits=["apple","banana","cherry", a = [100, 50, 65, 82, 23]
“kiwi","mango"] a.sort()
newlist = [] print(a)
for x in fruits: o/p:
if "a" in x: ---------------------------------------
newlist.append(x) Sort descending:
print(newlist) (Reverse=true)
output: a = ["orange", "mango", "kiwi",
"pineapple", "banana"]
[‘apple’,’banana’,’mango’] a.sort(reverse = True)
print(a)
with only one line of code: o/p:
newlist =[x for x in fruits if "a" in x] ['pineapple', 'orange', 'mango', 'kiwi',
'banana']
syntax: ---------------------------------------
newlist = [expression for item in a = [100, 50, 65, 82, 23]
iterable if condition == True] a.sort(reverse = True)
eg: print(a)
newlist = [x for x in fruits if x != o/p:
"apple"] ----------------------------------------
Iterable: Customize sort:
we use range function. Return:
newlist = [x for x in range(10)] The Python return statement is a key
o/p: component of functions and methods. You
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] can use the return statement to make
------------------------------------ your functions send Python objects back
newlist = [x for x in range(10) if x < to the caller code. These objects are
5] known as the function's return value.
o/p: [0, 1, 2, 3, 4] You can use them to perform further
computation in your programs.
Abs-returns the absolute value.
def myfunc(n): a reference to list1, and changes made
return abs(n - 50) in list1 will automatically also be made
in list2.
a = [100, 50, 65, 82, 23] There are ways to make a copy, one way
a.sort(key = myfunc) is to use the built-in List
print(a) method copy().
o/p: a = ["apple", "banana", "cherry"]
[50, 65, 23, 82, 100] mylist = a.copy()
------------------------------------ print(mylist)
For understanding: ---------------------------------------
def myfunc(n): list1 = ["a", "b", "c"]
return abs(n - 50) list2 = [1, 2, 3]
list3 = list1 + list2
a = [100, 50, 65, 82, 23] print(list3)
a=[] o/p:
b=[] ['a', 'b', 'c', 1, 2, 3]
print(a) ----------------------------------------
for x in a: list1 = ["a", "b" , "c"]
x=x-50 list2 = [1, 2, 3]
a.append(x) for x in list2:
print(a) list1.append(x)
for x in a: print(list1)
x=abs(x) o/p:
b.append(x) ----------------------------------------
print(b) list1 = ["a", "b" , "c"]
b.sort() list2 = [1, 2, 3]
print(b) list1.extend(list2)
a.sort(key = myfunc) print(list1)
print(a). o/P:
------------------------------------- ----------------------------------------
By default the sort() method is case
sensitive, resulting in all capital
letters being sorted before lower case
letters:
Example:
a = ["banana", "Orange", "Kiwi",
"cherry"]
a.sort()
print(a)
o/p:
['Kiwi', 'Orange', 'banana', 'cherry']
--------------------------------------
a = ["banana", "Orange", "Kiwi",
"cherry"]
a.sort(key = str.lower)
print(a)
o/p:
--------------------------------------
Copying a list:
You cannot copy a list simply by
typing list2 = list1,
because: list2 will only be
Problems on list:
Change the first and last element in a list.insert(0, last)
list. list.append(first)
#method1: return list
Swapping using a temp variable. # Driver code
Solution: newList = [12, 35, 9, 56, 24]
#define swap function print(swapList(newList))
def swaplist(newlist) ----------------------------------------
size = len(newlist) PERFECT NUMBER:
#swapping Given a number N. Check if it is perfect
temp=newlist[0] or not. A number is perfect if sum of
newlist[0]=newlist[size-1] divisors of its digit is equal to the
newlist[size-1]=temp given number.
retun newlist CODE:
#driver program
Newlist=[12,13,14,15,16,17] n=int(input("n= "))
Print(swaplist(newlist)) sum=0
o/p:[17,13,14,15,16,12] list=[]
for i in range(1,n):
#method2: if n%i==0:
#define swap function sum=sum+i
def swaplist(newlist) list.append(i)
newlist[0],newlist[-1]=newlist[- if(sum==n):
1],newlist[0] print("true\n")
return newlist print("divisors of",n, "are",list)
print("sum of the divisors is",n)
#method3: else:
#define swap function print("false")
def swaplist(list) print("divisors of",n, "are",list)
get= list[-1], list[0] print("sum of the divisors is not
list[0],list[-1]=get equal to",n)
return list O/P:
n= 6
#method4: true
Using * operand. divisors of 6 are [1, 2, 3]
list = [1, 2, 3, 4] sum of the divisors is 6
a, *b, c = list ----------------------------------------
print(a)
print(b)
print(c)
def swapList(list):
start, *middle, end = list
list = [end, *middle, start]
return list
# Driver code
newList = [12, 35, 9, 56, 24]
print(swapList(newList))

#method5:
def swapList(list):
first = list.pop(0)
last = list.pop(-1)

You might also like