Description
stringlengths 9
105
| Link
stringlengths 45
135
| Code
stringlengths 10
26.8k
| Test_Case
stringlengths 9
202
| Merge
stringlengths 63
27k
|
---|---|---|---|---|
Python program to interchange first and last elements in a list | https://www.geeksforgeeks.org/python-program-to-interchange-first-and-last-elements-in-a-list/ | # Python3 program to swap first
# and last element of a list
# Swap function
def swapList(newList):
size = len(newList)
# Swapping
temp = newList[0]
newList[0] = newList[size - 1]
newList[size - 1] = temp
return newList
# Driver code
newList = [12, 35, 9, 56, 24]
print(swapList(newList)) | #Input : [12, 35, 9, 56, 24]
#Output : [24, 35, 9, 56, 12] | Python program to interchange first and last elements in a list
# Python3 program to swap first
# and last element of a list
# Swap function
def swapList(newList):
size = len(newList)
# Swapping
temp = newList[0]
newList[0] = newList[size - 1]
newList[size - 1] = temp
return newList
# Driver code
newList = [12, 35, 9, 56, 24]
print(swapList(newList))
#Input : [12, 35, 9, 56, 24]
#Output : [24, 35, 9, 56, 12]
[END] |
Python program to interchange first and last elements in a list | https://www.geeksforgeeks.org/python-program-to-interchange-first-and-last-elements-in-a-list/ | # Python3 program to swap first
# and last element of a list
# Swap function
def swapList(newList):
newList[0], newList[-1] = newList[-1], newList[0]
return newList
# Driver code
newList = [12, 35, 9, 56, 24]
print(swapList(newList)) | #Input : [12, 35, 9, 56, 24]
#Output : [24, 35, 9, 56, 12] | Python program to interchange first and last elements in a list
# Python3 program to swap first
# and last element of a list
# Swap function
def swapList(newList):
newList[0], newList[-1] = newList[-1], newList[0]
return newList
# Driver code
newList = [12, 35, 9, 56, 24]
print(swapList(newList))
#Input : [12, 35, 9, 56, 24]
#Output : [24, 35, 9, 56, 12]
[END] |
Python program to interchange first and last elements in a list | https://www.geeksforgeeks.org/python-program-to-interchange-first-and-last-elements-in-a-list/ | # Python3 program to swap first
# and last element of a list
# Swap function
def swapList(list):
# Storing the first and last element
# as a pair in a tuple variable get
get = list[-1], list[0]
# unpacking those elements
list[0], list[-1] = get
return list
# Driver code
newList = [12, 35, 9, 56, 24]
print(swapList(newList)) | #Input : [12, 35, 9, 56, 24]
#Output : [24, 35, 9, 56, 12] | Python program to interchange first and last elements in a list
# Python3 program to swap first
# and last element of a list
# Swap function
def swapList(list):
# Storing the first and last element
# as a pair in a tuple variable get
get = list[-1], list[0]
# unpacking those elements
list[0], list[-1] = get
return list
# Driver code
newList = [12, 35, 9, 56, 24]
print(swapList(newList))
#Input : [12, 35, 9, 56, 24]
#Output : [24, 35, 9, 56, 12]
[END] |
Python program to interchange first and last elements in a list | https://www.geeksforgeeks.org/python-program-to-interchange-first-and-last-elements-in-a-list/ | # Python3 program to illustrate
# the usage of * operand
list = [1, 2, 3, 4]
a, *b, c = list
print(a)
print(b)
print(c) | #Input : [12, 35, 9, 56, 24]
#Output : [24, 35, 9, 56, 12] | Python program to interchange first and last elements in a list
# Python3 program to illustrate
# the usage of * operand
list = [1, 2, 3, 4]
a, *b, c = list
print(a)
print(b)
print(c)
#Input : [12, 35, 9, 56, 24]
#Output : [24, 35, 9, 56, 12]
[END] |
Python program to interchange first and last elements in a list | https://www.geeksforgeeks.org/python-program-to-interchange-first-and-last-elements-in-a-list/ | # Python3 program to swap first
# and last element of a list
# Swap function
def swapList(list):
start, *middle, end = list
list = [end, *middle, start]
return list
# Driver code
newList = [12, 35, 9, 56, 24]
print(swapList(newList)) | #Input : [12, 35, 9, 56, 24]
#Output : [24, 35, 9, 56, 12] | Python program to interchange first and last elements in a list
# Python3 program to swap first
# and last element of a list
# Swap function
def swapList(list):
start, *middle, end = list
list = [end, *middle, start]
return list
# Driver code
newList = [12, 35, 9, 56, 24]
print(swapList(newList))
#Input : [12, 35, 9, 56, 24]
#Output : [24, 35, 9, 56, 12]
[END] |
Python program to interchange first and last elements in a list | https://www.geeksforgeeks.org/python-program-to-interchange-first-and-last-elements-in-a-list/ | # Python3 program to swap first
# and last element of a list
# Swap function
def swapList(list):
first = list.pop(0)
last = list.pop(-1)
list.insert(0, last)
list.append(first)
return list
# Driver code
newList = [12, 35, 9, 56, 24]
print(swapList(newList)) | #Input : [12, 35, 9, 56, 24]
#Output : [24, 35, 9, 56, 12] | Python program to interchange first and last elements in a list
# Python3 program to swap first
# and last element of a list
# Swap function
def swapList(list):
first = list.pop(0)
last = list.pop(-1)
list.insert(0, last)
list.append(first)
return list
# Driver code
newList = [12, 35, 9, 56, 24]
print(swapList(newList))
#Input : [12, 35, 9, 56, 24]
#Output : [24, 35, 9, 56, 12]
[END] |
Python program to swap two elements in a list | https://www.geeksforgeeks.org/python-program-to-swap-two-elements-in-a-list/ | # Python3 program to swap elements
# at given positions
# Swap function
def swapPositions(list, pos1, pos2):
list[pos1], list[pos2] = list[pos2], list[pos1]
return list
# Driver function
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1 - 1, pos2 - 1)) |
#Output : [19, 65, 23, 90] | Python program to swap two elements in a list
# Python3 program to swap elements
# at given positions
# Swap function
def swapPositions(list, pos1, pos2):
list[pos1], list[pos2] = list[pos2], list[pos1]
return list
# Driver function
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1 - 1, pos2 - 1))
#Output : [19, 65, 23, 90]
[END] |
Python program to swap two elements in a list | https://www.geeksforgeeks.org/python-program-to-swap-two-elements-in-a-list/ | # Python3 program to swap elements
# at given positions
# Swap function
def swapPositions(list, pos1, pos2):
# popping both the elements from list
first_ele = list.pop(pos1)
second_ele = list.pop(pos2 - 1)
# inserting in each others positions
list.insert(pos1, second_ele)
list.insert(pos2, first_ele)
return list
# Driver function
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1 - 1, pos2 - 1)) |
#Output : [19, 65, 23, 90] | Python program to swap two elements in a list
# Python3 program to swap elements
# at given positions
# Swap function
def swapPositions(list, pos1, pos2):
# popping both the elements from list
first_ele = list.pop(pos1)
second_ele = list.pop(pos2 - 1)
# inserting in each others positions
list.insert(pos1, second_ele)
list.insert(pos2, first_ele)
return list
# Driver function
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1 - 1, pos2 - 1))
#Output : [19, 65, 23, 90]
[END] |
Python program to swap two elements in a list | https://www.geeksforgeeks.org/python-program-to-swap-two-elements-in-a-list/ | # Python3 program to swap elements at
# given positions
# Swap function
def swapPositions(list, pos1, pos2):
# Storing the two elements
# as a pair in a tuple variable get
get = list[pos1], list[pos2]
# unpacking those elements
list[pos2], list[pos1] = get
return list
# Driver Code
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1 - 1, pos2 - 1)) |
#Output : [19, 65, 23, 90] | Python program to swap two elements in a list
# Python3 program to swap elements at
# given positions
# Swap function
def swapPositions(list, pos1, pos2):
# Storing the two elements
# as a pair in a tuple variable get
get = list[pos1], list[pos2]
# unpacking those elements
list[pos2], list[pos1] = get
return list
# Driver Code
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1 - 1, pos2 - 1))
#Output : [19, 65, 23, 90]
[END] |
Python program to swap two elements in a list | https://www.geeksforgeeks.org/python-program-to-swap-two-elements-in-a-list/ | # Python3 program to swap elements
# at given positions
# Swap function
def swapPositions(lis, pos1, pos2):
temp = lis[pos1]
lis[pos1] = lis[pos2]
lis[pos2] = temp
return lis
# Driver function
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1 - 1, pos2 - 1)) |
#Output : [19, 65, 23, 90] | Python program to swap two elements in a list
# Python3 program to swap elements
# at given positions
# Swap function
def swapPositions(lis, pos1, pos2):
temp = lis[pos1]
lis[pos1] = lis[pos2]
lis[pos2] = temp
return lis
# Driver function
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1 - 1, pos2 - 1))
#Output : [19, 65, 23, 90]
[END] |
Python program to swap two elements in a list | https://www.geeksforgeeks.org/python-program-to-swap-two-elements-in-a-list/ | def swapPositions(lis, pos1, pos2):
for i, x in enumerate(lis):
if i == pos1:
elem1 = x
if i == pos2:
elem2 = x
lis[pos1] = elem2
lis[pos2] = elem1
return lis
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1 - 1, pos2 - 1))
# This code is contributed by Edula Vinay Kumar Reddy |
#Output : [19, 65, 23, 90] | Python program to swap two elements in a list
def swapPositions(lis, pos1, pos2):
for i, x in enumerate(lis):
if i == pos1:
elem1 = x
if i == pos2:
elem2 = x
lis[pos1] = elem2
lis[pos2] = elem1
return lis
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1 - 1, pos2 - 1))
# This code is contributed by Edula Vinay Kumar Reddy
#Output : [19, 65, 23, 90]
[END] |
Python - Swap elements in String List | https://www.geeksforgeeks.org/python-swap-elements-in-string-list/ | # Python3 code to demonstrate
# Swap elements in String list
# using replace() + list comprehension
# Initializing list
test_list = ["Gfg", "is", "best", "for", "Geeks"]
# printing original lists
print("The original list is : " + str(test_list))
# Swap elements in String list
# using replace() + list comprehension
res = [sub.replace("G", "-").replace("e", "G").replace("-", "e") for sub in test_list]
# printing result
print("List after performing character swaps : " + str(res)) |
#Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks'] | Python - Swap elements in String List
# Python3 code to demonstrate
# Swap elements in String list
# using replace() + list comprehension
# Initializing list
test_list = ["Gfg", "is", "best", "for", "Geeks"]
# printing original lists
print("The original list is : " + str(test_list))
# Swap elements in String list
# using replace() + list comprehension
res = [sub.replace("G", "-").replace("e", "G").replace("-", "e") for sub in test_list]
# printing result
print("List after performing character swaps : " + str(res))
#Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks']
[END] |
Python - Swap elements in String List | https://www.geeksforgeeks.org/python-swap-elements-in-string-list/ | # Python3 code to demonstrate
# Swap elements in String list
# using replace() + join() + split()
# Initializing list
test_list = ["Gfg", "is", "best", "for", "Geeks"]
# printing original lists
print("The original list is : " + str(test_list))
# Swap elements in String list
# using replace() + join() + split()
res = ", ".join(test_list)
res = res.replace("G", "_").replace("e", "G").replace("_", "e").split(", ")
# printing result
print("List after performing character swaps : " + str(res)) |
#Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks'] | Python - Swap elements in String List
# Python3 code to demonstrate
# Swap elements in String list
# using replace() + join() + split()
# Initializing list
test_list = ["Gfg", "is", "best", "for", "Geeks"]
# printing original lists
print("The original list is : " + str(test_list))
# Swap elements in String list
# using replace() + join() + split()
res = ", ".join(test_list)
res = res.replace("G", "_").replace("e", "G").replace("_", "e").split(", ")
# printing result
print("List after performing character swaps : " + str(res))
#Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks']
[END] |
Python - Swap elements in String List | https://www.geeksforgeeks.org/python-swap-elements-in-string-list/ | # Python3 code to demonstrate
# Swap elements in String list
# using regex
# Initializing list
import re
test_list = ["Gfg", "is", "best", "for", "Geeks"]
# printing original lists
print("The original list is : " + str(test_list))
# Swap elements in String list
# using regex
res = [re.sub("-", "e", re.sub("e", "G", re.sub("G", "-", sub))) for sub in test_list]
# printing result
print("List after performing character swaps : " + str(res)) |
#Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks'] | Python - Swap elements in String List
# Python3 code to demonstrate
# Swap elements in String list
# using regex
# Initializing list
import re
test_list = ["Gfg", "is", "best", "for", "Geeks"]
# printing original lists
print("The original list is : " + str(test_list))
# Swap elements in String list
# using regex
res = [re.sub("-", "e", re.sub("e", "G", re.sub("G", "-", sub))) for sub in test_list]
# printing result
print("List after performing character swaps : " + str(res))
#Output : The original list is : ['Gfg', 'is', 'best', 'for', 'Geeks']
[END] |
Python | Ways to find length of list | https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/ | # Python len()
li = [10, 20, 30]
n = len(li)
print("The length of list is: ", n) |
#Output : The length of list is: 3 | Python | Ways to find length of list
# Python len()
li = [10, 20, 30]
n = len(li)
print("The length of list is: ", n)
#Output : The length of list is: 3
[END] |
Python | Ways to find length of list | https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/ | # Python code to demonstrate
# length of list
# using naive method
# Initializing list
test_list = [1, 4, 5, 7, 8]
# Printing test_list
print("The list is : " + str(test_list))
# Finding length of list
# using loop
# Initializing counter
counter = 0
for i in test_list:
# incrementing counter
counter = counter + 1
# Printing length of list
print("Length of list using naive method is : " + str(counter)) |
#Output : The length of list is: 3 | Python | Ways to find length of list
# Python code to demonstrate
# length of list
# using naive method
# Initializing list
test_list = [1, 4, 5, 7, 8]
# Printing test_list
print("The list is : " + str(test_list))
# Finding length of list
# using loop
# Initializing counter
counter = 0
for i in test_list:
# incrementing counter
counter = counter + 1
# Printing length of list
print("Length of list using naive method is : " + str(counter))
#Output : The length of list is: 3
[END] |
Python | Ways to find length of list | https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/ | # Python program to demonstrate working
# of len()
a = []
a.append("Hello")
a.append("Geeks")
a.append("For")
a.append("Geeks")
print("The length of list is: ", len(a)) |
#Output : The length of list is: 3 | Python | Ways to find length of list
# Python program to demonstrate working
# of len()
a = []
a.append("Hello")
a.append("Geeks")
a.append("For")
a.append("Geeks")
print("The length of list is: ", len(a))
#Output : The length of list is: 3
[END] |
Python | Ways to find length of list | https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/ | # Python code to demonstrate
# length of list
# using len() and length_hint
from operator import length_hint
# Initializing list
test_list = [1, 4, 5, 7, 8]
# Printing test_list
print("The list is : " + str(test_list))
# Finding length of list
# using len()
list_len = len(test_list)
# Finding length of list
# using length_hint()
list_len_hint = length_hint(test_list)
# Printing length of list
print("Length of list using len() is : " + str(list_len))
print("Length of list using length_hint() is : " + str(list_len_hint)) |
#Output : The length of list is: 3 | Python | Ways to find length of list
# Python code to demonstrate
# length of list
# using len() and length_hint
from operator import length_hint
# Initializing list
test_list = [1, 4, 5, 7, 8]
# Printing test_list
print("The list is : " + str(test_list))
# Finding length of list
# using len()
list_len = len(test_list)
# Finding length of list
# using length_hint()
list_len_hint = length_hint(test_list)
# Printing length of list
print("Length of list using len() is : " + str(list_len))
print("Length of list using length_hint() is : " + str(list_len_hint))
#Output : The length of list is: 3
[END] |
Python | Ways to find length of list | https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/ | from operator import length_hint
import time
# Initializing list
test_list = [1, 4, 5, 7, 8]
# Printing test_list
print("The list is : " + str(test_list))
# Finding length of list
# using loop
# Initializing counter
start_time_naive = time.time()
counter = 0
for i in test_list:
# incrementing counter
counter = counter + 1
end_time_naive = str(time.time() - start_time_naive)
# Finding length of list
# using len()
start_time_len = time.time()
list_len = len(test_list)
end_time_len = str(time.time() - start_time_len)
# Finding length of list
# using length_hint()
start_time_hint = time.time()
list_len_hint = length_hint(test_list)
end_time_hint = str(time.time() - start_time_hint)
# Printing Times of each
print("Time taken using naive method is : " + end_time_naive)
print("Time taken using len() is : " + end_time_len)
print("Time taken using length_hint() is : " + end_time_hint) |
#Output : The length of list is: 3 | Python | Ways to find length of list
from operator import length_hint
import time
# Initializing list
test_list = [1, 4, 5, 7, 8]
# Printing test_list
print("The list is : " + str(test_list))
# Finding length of list
# using loop
# Initializing counter
start_time_naive = time.time()
counter = 0
for i in test_list:
# incrementing counter
counter = counter + 1
end_time_naive = str(time.time() - start_time_naive)
# Finding length of list
# using len()
start_time_len = time.time()
list_len = len(test_list)
end_time_len = str(time.time() - start_time_len)
# Finding length of list
# using length_hint()
start_time_hint = time.time()
list_len_hint = length_hint(test_list)
end_time_hint = str(time.time() - start_time_hint)
# Printing Times of each
print("Time taken using naive method is : " + end_time_naive)
print("Time taken using len() is : " + end_time_len)
print("Time taken using length_hint() is : " + end_time_hint)
#Output : The length of list is: 3
[END] |
Python | Ways to find length of list | https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/ | # Python code to demonstrate
# length of list
# using sum()
# Initializing list
test_list = [1, 4, 5, 7, 8]
# Printing test_list
print("The list is : " + str(test_list))
# Finding length of list
# using sum()
list_len = sum(1 for i in test_list)
# Printing length of list
print("Length of list using len() is : " + str(list_len))
print("Length of list using length_hint() is : " + str(list_len)) |
#Output : The length of list is: 3 | Python | Ways to find length of list
# Python code to demonstrate
# length of list
# using sum()
# Initializing list
test_list = [1, 4, 5, 7, 8]
# Printing test_list
print("The list is : " + str(test_list))
# Finding length of list
# using sum()
list_len = sum(1 for i in test_list)
# Printing length of list
print("Length of list using len() is : " + str(list_len))
print("Length of list using length_hint() is : " + str(list_len))
#Output : The length of list is: 3
[END] |
Python | Ways to find length of list | https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/ | # python code to find the length
# of list using enumerate function
list1 = [1, 4, 5, 7, 8]
s = 0
for i, a in enumerate(list1):
s += 1
print(s) |
#Output : The length of list is: 3 | Python | Ways to find length of list
# python code to find the length
# of list using enumerate function
list1 = [1, 4, 5, 7, 8]
s = 0
for i, a in enumerate(list1):
s += 1
print(s)
#Output : The length of list is: 3
[END] |
Python | Ways to find length of list | https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/ | from collections import Counter
# Initializing list
test_list = [1, 4, 5, 7, 8]
# Finding length of list using Counter()
list_len = sum(Counter(test_list).values())
print("Length of list using Counter() is:", list_len)
# This code is contributed by Edula Vinay Kumar Reddy |
#Output : The length of list is: 3 | Python | Ways to find length of list
from collections import Counter
# Initializing list
test_list = [1, 4, 5, 7, 8]
# Finding length of list using Counter()
list_len = sum(Counter(test_list).values())
print("Length of list using Counter() is:", list_len)
# This code is contributed by Edula Vinay Kumar Reddy
#Output : The length of list is: 3
[END] |
Python | Ways to find length of list | https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/ | # Define the list to be used for the demonstration
test_list = [1, 4, 5, 7, 8]
# Calculate the length of the list using a list comprehension and the sum function
# The list comprehension generates a sequence of ones for each element in the list
# The sum function then sums all the ones to give the length of the list
length = sum(1 for _ in test_list)
# Print the length of the list
print("Length of list using list comprehension is:", length) |
#Output : The length of list is: 3 | Python | Ways to find length of list
# Define the list to be used for the demonstration
test_list = [1, 4, 5, 7, 8]
# Calculate the length of the list using a list comprehension and the sum function
# The list comprehension generates a sequence of ones for each element in the list
# The sum function then sums all the ones to give the length of the list
length = sum(1 for _ in test_list)
# Print the length of the list
print("Length of list using list comprehension is:", length)
#Output : The length of list is: 3
[END] |
Python | Ways to find length of list | https://www.geeksforgeeks.org/python-ways-to-find-length-of-list/ | # Define a function to count the number of elements in a list using recursion
def count_elements_recursion(lst):
# Base case: if the list is empty, return 0
if not lst:
return 0
# Recursive case: add 1 to the count of the remaining elements in the list
return 1 + count_elements_recursion(lst[1:])
# Test the function with a sample list
lst = [1, 2, 3, 4, 5]
print("The length of the list is:", count_elements_recursion(lst))
# Output: The length of the list is: 5 |
#Output : The length of list is: 3 | Python | Ways to find length of list
# Define a function to count the number of elements in a list using recursion
def count_elements_recursion(lst):
# Base case: if the list is empty, return 0
if not lst:
return 0
# Recursive case: add 1 to the count of the remaining elements in the list
return 1 + count_elements_recursion(lst[1:])
# Test the function with a sample list
lst = [1, 2, 3, 4, 5]
print("The length of the list is:", count_elements_recursion(lst))
# Output: The length of the list is: 5
#Output : The length of list is: 3
[END] |
Maximum of two numbers in Python | https://www.geeksforgeeks.org/maximum-of-two-numbers-in-python/?ref=leftbar-rightbar | # Python program to find the
# maximum of two numbers
def maximum(a, b):
if a >= b:
return a
else:
return b
# Driver code
a = 2
b = 4
print(maximum(a, b)) |
#Output : 4 | Maximum of two numbers in Python
# Python program to find the
# maximum of two numbers
def maximum(a, b):
if a >= b:
return a
else:
return b
# Driver code
a = 2
b = 4
print(maximum(a, b))
#Output : 4
[END] |
Maximum of two numbers in Python | https://www.geeksforgeeks.org/maximum-of-two-numbers-in-python/?ref=leftbar-rightbar | # Python program to find the
# maximum of two numbers
a = 2
b = 4
maximum = max(a, b)
print(maximum) |
#Output : 4 | Maximum of two numbers in Python
# Python program to find the
# maximum of two numbers
a = 2
b = 4
maximum = max(a, b)
print(maximum)
#Output : 4
[END] |
Maximum of two numbers in Python | https://www.geeksforgeeks.org/maximum-of-two-numbers-in-python/?ref=leftbar-rightbar | # Python program to find the
# maximum of two numbers
# Driver code
a = 2
b = 4
# Use of ternary operator
print(a if a >= b else b)
# This code is contributed by AnkThon |
#Output : 4 | Maximum of two numbers in Python
# Python program to find the
# maximum of two numbers
# Driver code
a = 2
b = 4
# Use of ternary operator
print(a if a >= b else b)
# This code is contributed by AnkThon
#Output : 4
[END] |
Maximum of two numbers in Python | https://www.geeksforgeeks.org/maximum-of-two-numbers-in-python/?ref=leftbar-rightbar | # python code to find maximum of two numbers
a = 2
b = 4
maximum = lambda a, b: a if a > b else b
print(f"{maximum(a,b)} is a maximum number") |
#Output : 4 | Maximum of two numbers in Python
# python code to find maximum of two numbers
a = 2
b = 4
maximum = lambda a, b: a if a > b else b
print(f"{maximum(a,b)} is a maximum number")
#Output : 4
[END] |
Maximum of two numbers in Python | https://www.geeksforgeeks.org/maximum-of-two-numbers-in-python/?ref=leftbar-rightbar | a = 2
b = 4
x = [a if a > b else b]
print("maximum number is:", x) |
#Output : 4 | Maximum of two numbers in Python
a = 2
b = 4
x = [a if a > b else b]
print("maximum number is:", x)
#Output : 4
[END] |
Maximum of two numbers in Python | https://www.geeksforgeeks.org/maximum-of-two-numbers-in-python/?ref=leftbar-rightbar | # Python program to find the
# maximum of two numbers
a = 2
b = 4
x = [a, b]
x.sort()
print(x[-1]) |
#Output : 4 | Maximum of two numbers in Python
# Python program to find the
# maximum of two numbers
a = 2
b = 4
x = [a, b]
x.sort()
print(x[-1])
#Output : 4
[END] |
Minimum of two numbers in Python | https://www.geeksforgeeks.org/minimum-of-two-numbers-in-python/ | # Python program to find the
# minimum of two numbers
def minimum(a, b):
if a <= b:
return a
else:
return b
# Driver code
a = 2
b = 4
print(minimum(a, b)) | Input: a = 2, b = 4
Output: 2 | Minimum of two numbers in Python
# Python program to find the
# minimum of two numbers
def minimum(a, b):
if a <= b:
return a
else:
return b
# Driver code
a = 2
b = 4
print(minimum(a, b))
Input: a = 2, b = 4
Output: 2
[END] |
Minimum of two numbers in Python | https://www.geeksforgeeks.org/minimum-of-two-numbers-in-python/ | # Python program to find the
# minimum of two numbers
a = 2
b = 4
minimum = min(a, b)
print(minimum) | Input: a = 2, b = 4
Output: 2 | Minimum of two numbers in Python
# Python program to find the
# minimum of two numbers
a = 2
b = 4
minimum = min(a, b)
print(minimum)
Input: a = 2, b = 4
Output: 2
[END] |
Minimum of two numbers in Python | https://www.geeksforgeeks.org/minimum-of-two-numbers-in-python/ | a = 2
b = 4
print(sorted([a, b])[0]) | Input: a = 2, b = 4
Output: 2 | Minimum of two numbers in Python
a = 2
b = 4
print(sorted([a, b])[0])
Input: a = 2, b = 4
Output: 2
[END] |
Python | Ways to check if element exists in list | https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/ | # python code to Check if element exists in list or not
lst = [1, 6, 3, 5, 3, 4]
# checking if element 7 is present
# in the given list or not
i = 7
# if element present then return
# exist otherwise not exist
if i in lst:
print("exist")
else:
print("not exist")
# this code is contributed by gangarajula laxmi | Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4] | Python | Ways to check if element exists in list
# python code to Check if element exists in list or not
lst = [1, 6, 3, 5, 3, 4]
# checking if element 7 is present
# in the given list or not
i = 7
# if element present then return
# exist otherwise not exist
if i in lst:
print("exist")
else:
print("not exist")
# this code is contributed by gangarajula laxmi
Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4]
[END] |
Python | Ways to check if element exists in list | https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/ | # Initializing list
test_list = [1, 6, 3, 5, 3, 4]
# Checking if 4 exists in list
for i in test_list:
if i == 4:
print("Element Exists") | Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4] | Python | Ways to check if element exists in list
# Initializing list
test_list = [1, 6, 3, 5, 3, 4]
# Checking if 4 exists in list
for i in test_list:
if i == 4:
print("Element Exists")
Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4]
[END] |
Python | Ways to check if element exists in list | https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/ | # Initializing list
test_list = [1, 6, 3, 5, 3, 4]
# Checking if 4 exists in list
# using in
if 4 in test_list:
print("Element Exists") | Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4] | Python | Ways to check if element exists in list
# Initializing list
test_list = [1, 6, 3, 5, 3, 4]
# Checking if 4 exists in list
# using in
if 4 in test_list:
print("Element Exists")
Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4]
[END] |
Python | Ways to check if element exists in list | https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/ | # Initializing list
test_list = [1, 6, 3, 5, 3, 4]
result = any(item in test_list for item in test_list)
print("Does string contain any list element : " + str(bool(result))) | Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4] | Python | Ways to check if element exists in list
# Initializing list
test_list = [1, 6, 3, 5, 3, 4]
result = any(item in test_list for item in test_list)
print("Does string contain any list element : " + str(bool(result)))
Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4]
[END] |
Python | Ways to check if element exists in list | https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/ | # Initializing list
test_list = [10, 15, 20, 7, 46, 2808]
print("Checking if 15 exists in list")
# number of times element exists in list
exist_count = test_list.count(15)
# checking if it is more than 0
if exist_count > 0:
print("Yes, 15 exists in list")
else:
print("No, 15 does not exists in list") | Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4] | Python | Ways to check if element exists in list
# Initializing list
test_list = [10, 15, 20, 7, 46, 2808]
print("Checking if 15 exists in list")
# number of times element exists in list
exist_count = test_list.count(15)
# checking if it is more than 0
if exist_count > 0:
print("Yes, 15 exists in list")
else:
print("No, 15 does not exists in list")
Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4]
[END] |
Python | Ways to check if element exists in list | https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/ | from bisect import bisect_left, bisect
# Initializing list
test_list_set = [1, 6, 3, 5, 3, 4]
test_list_bisect = [1, 6, 3, 5, 3, 4]
print("Checking if 4 exists in list ( using set() + in) : ")
# Checking if 4 exists in list
# using set() + in
test_list_set = set(test_list_set)
if 4 in test_list_set:
print("Element Exists")
print("Checking if 4 exists in list ( using sort() + bisect_left() ) : ")
# Checking if 4 exists in list
# using sort() + bisect_left()
test_list_bisect.sort()
if bisect_left(test_list_bisect, 4) != bisect(test_list_bisect, 4):
print("Element Exists")
else:
print("Element doesnt exist") | Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4] | Python | Ways to check if element exists in list
from bisect import bisect_left, bisect
# Initializing list
test_list_set = [1, 6, 3, 5, 3, 4]
test_list_bisect = [1, 6, 3, 5, 3, 4]
print("Checking if 4 exists in list ( using set() + in) : ")
# Checking if 4 exists in list
# using set() + in
test_list_set = set(test_list_set)
if 4 in test_list_set:
print("Element Exists")
print("Checking if 4 exists in list ( using sort() + bisect_left() ) : ")
# Checking if 4 exists in list
# using sort() + bisect_left()
test_list_bisect.sort()
if bisect_left(test_list_bisect, 4) != bisect(test_list_bisect, 4):
print("Element Exists")
else:
print("Element doesnt exist")
Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4]
[END] |
Python | Ways to check if element exists in list | https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/ | # Initializing list
test_list = [10, 15, 20, 7, 46, 2808]
print("Checking if 15 exists in list")
x = list(map(str, test_list))
y = "-".join(x)
if y.find("15") != -1:
print("Yes, 15 exists in list")
else:
print("No, 15 does not exists in list") | Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4] | Python | Ways to check if element exists in list
# Initializing list
test_list = [10, 15, 20, 7, 46, 2808]
print("Checking if 15 exists in list")
x = list(map(str, test_list))
y = "-".join(x)
if y.find("15") != -1:
print("Yes, 15 exists in list")
else:
print("No, 15 does not exists in list")
Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4]
[END] |
Python | Ways to check if element exists in list | https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/ | from collections import Counter
test_list = [10, 15, 20, 7, 46, 2808]
# Calculating frequencies
frequency = Counter(test_list)
# If the element has frequency greater than 0
# then it exists else it doesn't exist
if frequency[15] > 0:
print("Yes, 15 exists in list")
else:
print("No, 15 does not exists in list")
# This code is contributed by vikkycirus | Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4] | Python | Ways to check if element exists in list
from collections import Counter
test_list = [10, 15, 20, 7, 46, 2808]
# Calculating frequencies
frequency = Counter(test_list)
# If the element has frequency greater than 0
# then it exists else it doesn't exist
if frequency[15] > 0:
print("Yes, 15 exists in list")
else:
print("No, 15 does not exists in list")
# This code is contributed by vikkycirus
Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4]
[END] |
Python | Ways to check if element exists in list | https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/ | def element_exists(lst, element):
# Try to get the index of the element in the list
try:
lst.index(element)
# If the element is found, return True
return True
# If a ValueError is raised, the element is not in the list
except ValueError:
# Return False in this case
return False
# Test the function
test_list = [1, 6, 3, 5, 3, 4]
print(element_exists(test_list, 3)) # prints True
print(element_exists(test_list, 7)) # prints False
# This code is contributed by Edula Vinay Kumar Reddy | Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4] | Python | Ways to check if element exists in list
def element_exists(lst, element):
# Try to get the index of the element in the list
try:
lst.index(element)
# If the element is found, return True
return True
# If a ValueError is raised, the element is not in the list
except ValueError:
# Return False in this case
return False
# Test the function
test_list = [1, 6, 3, 5, 3, 4]
print(element_exists(test_list, 3)) # prints True
print(element_exists(test_list, 7)) # prints False
# This code is contributed by Edula Vinay Kumar Reddy
Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4]
[END] |
Python | Ways to check if element exists in list | https://www.geeksforgeeks.org/python-ways-to-check-if-element-exists-in-list/ | def check_element_exists_set(lst, target):
return target in set(lst)
# Example
test_list = [1, 6, 3, 5, 3, 4]
target = 3
print("Exists using set: ", check_element_exists_set(test_list, target)) | Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4] | Python | Ways to check if element exists in list
def check_element_exists_set(lst, target):
return target in set(lst)
# Example
test_list = [1, 6, 3, 5, 3, 4]
target = 3
print("Exists using set: ", check_element_exists_set(test_list, target))
Input: 3 # Check if 3 exist or not.
list = test_list = [1, 6, 3, 5, 3, 4]
[END] |
Different ways to clear a list in Python | https://www.geeksforgeeks.org/different-ways-to-clear-a-list-in-python/ | # Python program to clear a list
# using clear() method
# Creating list
GEEK = [6, 0, 4, 1]
print("GEEK before clear:", GEEK)
# Clearing list
GEEK.clear()
print("GEEK after clear:", GEEK) |
#Output : GEEK before clear: [6, 0, 4, 1] | Different ways to clear a list in Python
# Python program to clear a list
# using clear() method
# Creating list
GEEK = [6, 0, 4, 1]
print("GEEK before clear:", GEEK)
# Clearing list
GEEK.clear()
print("GEEK after clear:", GEEK)
#Output : GEEK before clear: [6, 0, 4, 1]
[END] |
Different ways to clear a list in Python | https://www.geeksforgeeks.org/different-ways-to-clear-a-list-in-python/ | # Python3 code to demonstrate
# clearing a list using
# clear and Reinitializing
# Initializing lists
list1 = [1, 2, 3]
list2 = [5, 6, 7]
# Printing list1 before deleting
print("List1 before deleting is : " + str(list1))
# deleting list using clear()
list1.clear()
# Printing list1 after clearing
print("List1 after clearing using clear() : " + str(list1))
# Printing list2 before deleting
print("List2 before deleting is : " + str(list2))
# deleting list using reinitialization
list2 = []
# Printing list2 after reinitialization
print("List2 after clearing using reinitialization : " + str(list2)) |
#Output : GEEK before clear: [6, 0, 4, 1] | Different ways to clear a list in Python
# Python3 code to demonstrate
# clearing a list using
# clear and Reinitializing
# Initializing lists
list1 = [1, 2, 3]
list2 = [5, 6, 7]
# Printing list1 before deleting
print("List1 before deleting is : " + str(list1))
# deleting list using clear()
list1.clear()
# Printing list1 after clearing
print("List1 after clearing using clear() : " + str(list1))
# Printing list2 before deleting
print("List2 before deleting is : " + str(list2))
# deleting list using reinitialization
list2 = []
# Printing list2 after reinitialization
print("List2 after clearing using reinitialization : " + str(list2))
#Output : GEEK before clear: [6, 0, 4, 1]
[END] |
Different ways to clear a list in Python | https://www.geeksforgeeks.org/different-ways-to-clear-a-list-in-python/ | # Python3 code to demonstrate
# clearing a list using
# clear and Reinitializing
# Initializing lists
list1 = [1, 2, 3]
list2 = [5, 6, 7]
# Printing list1 before deleting
print("List1 before deleting is : " + str(list1))
# deleting list using clear()
list1.clear()
# Printing list1 after clearing
print("List1 after clearing using clear() : " + str(list1))
# Printing list2 before deleting
print("List2 before deleting is : " + str(list2))
# deleting list using reinitialization
list2 = []
# Printing list2 after reinitialization
print("List2 after clearing using reinitialization : " + str(list2)) |
#Output : GEEK before clear: [6, 0, 4, 1] | Different ways to clear a list in Python
# Python3 code to demonstrate
# clearing a list using
# clear and Reinitializing
# Initializing lists
list1 = [1, 2, 3]
list2 = [5, 6, 7]
# Printing list1 before deleting
print("List1 before deleting is : " + str(list1))
# deleting list using clear()
list1.clear()
# Printing list1 after clearing
print("List1 after clearing using clear() : " + str(list1))
# Printing list2 before deleting
print("List2 before deleting is : " + str(list2))
# deleting list using reinitialization
list2 = []
# Printing list2 after reinitialization
print("List2 after clearing using reinitialization : " + str(list2))
#Output : GEEK before clear: [6, 0, 4, 1]
[END] |
Different ways to clear a list in Python | https://www.geeksforgeeks.org/different-ways-to-clear-a-list-in-python/ | # Python3 code to demonstrate
# clearing a list using
# del method
# Initializing lists
list1 = [1, 2, 3]
list2 = [5, 6, 7]
# Printing list1 before deleting
print("List1 before deleting is : " + str(list1))
# deleting list1 using del
del list1[:]
print("List1 after clearing using del : " + str(list1))
# Printing list2 before deleting
print("List2 before deleting is : " + str(list2))
# deleting list using del
del list2[:]
print("List2 after clearing using del : " + str(list2)) |
#Output : GEEK before clear: [6, 0, 4, 1] | Different ways to clear a list in Python
# Python3 code to demonstrate
# clearing a list using
# del method
# Initializing lists
list1 = [1, 2, 3]
list2 = [5, 6, 7]
# Printing list1 before deleting
print("List1 before deleting is : " + str(list1))
# deleting list1 using del
del list1[:]
print("List1 after clearing using del : " + str(list1))
# Printing list2 before deleting
print("List2 before deleting is : " + str(list2))
# deleting list using del
del list2[:]
print("List2 after clearing using del : " + str(list2))
#Output : GEEK before clear: [6, 0, 4, 1]
[END] |
Different ways to clear a list in Python | https://www.geeksforgeeks.org/different-ways-to-clear-a-list-in-python/ | # Python3 code to demonstrate
# Initializing lists
list1 = [1, 2, 3]
# Printing list1 before deleting
print("List1 before deleting is : " + str(list1))
# deleting list1
while len(list1) != 0:
list1.pop()
print("List1 after clearing using del : " + str(list1)) |
#Output : GEEK before clear: [6, 0, 4, 1] | Different ways to clear a list in Python
# Python3 code to demonstrate
# Initializing lists
list1 = [1, 2, 3]
# Printing list1 before deleting
print("List1 before deleting is : " + str(list1))
# deleting list1
while len(list1) != 0:
list1.pop()
print("List1 after clearing using del : " + str(list1))
#Output : GEEK before clear: [6, 0, 4, 1]
[END] |
Different ways to clear a list in Python | https://www.geeksforgeeks.org/different-ways-to-clear-a-list-in-python/ | # Initializing list
lst = [1, 2, 3, 4, 5]
# Clearing list using slicing
lst = lst[:0]
print(lst) # Output: []
# This code is contributed by Edula Vinay Kumar Reddy |
#Output : GEEK before clear: [6, 0, 4, 1] | Different ways to clear a list in Python
# Initializing list
lst = [1, 2, 3, 4, 5]
# Clearing list using slicing
lst = lst[:0]
print(lst) # Output: []
# This code is contributed by Edula Vinay Kumar Reddy
#Output : GEEK before clear: [6, 0, 4, 1]
[END] |
Different ways to clear a list in Python | https://www.geeksforgeeks.org/different-ways-to-clear-a-list-in-python/ | # Python3 code to demonstrate
# Initializing lists
list1 = [1, 2, 3]
# Printing list1 before deleting
print("List1 before deleting is : " + str(list1))
# Clearing list1 by assigning an empty list
list1 = []
print("List1 after clearing using an empty list : " + str(list1)) |
#Output : GEEK before clear: [6, 0, 4, 1] | Different ways to clear a list in Python
# Python3 code to demonstrate
# Initializing lists
list1 = [1, 2, 3]
# Printing list1 before deleting
print("List1 before deleting is : " + str(list1))
# Clearing list1 by assigning an empty list
list1 = []
print("List1 after clearing using an empty list : " + str(list1))
#Output : GEEK before clear: [6, 0, 4, 1]
[END] |
Python | Reversing a List | https://www.geeksforgeeks.org/python-reversing-list/ | # Python program to reverse an array
def list_reverse(arr, size):
# if only one element present, then return the array
if size == 1:
return arr
# if only two elements present, then swap both the numbers.
elif size == 2:
(
arr[0],
arr[1],
) = (
arr[1],
arr[0],
)
return arr
# if more than two elements presents, then swap first and last numbers.
else:
i = 0
while i < size // 2:
# swap present and preceding numbers at time and jump to second element after swap
arr[i], arr[size - i - 1] = arr[size - i - 1], arr[i]
# skip if present and preceding numbers indexes are same
if (i != i + 1 and size - i - 1 != size - i - 2) and (
i != size - i - 2 and size - i - 1 != i + 1
):
arr[i + 1], arr[size - i - 2] = arr[size - i - 2], arr[i + 1]
i += 2
return arr
arr = [1, 2, 3, 4, 5]
size = 5
print("Original list: ", arr)
print("Reversed list: ", list_reverse(arr, size))
# This contributed by SR.Dhanush | Input: arr[], size
1)if length of array is 1, then return arr. | Python | Reversing a List
# Python program to reverse an array
def list_reverse(arr, size):
# if only one element present, then return the array
if size == 1:
return arr
# if only two elements present, then swap both the numbers.
elif size == 2:
(
arr[0],
arr[1],
) = (
arr[1],
arr[0],
)
return arr
# if more than two elements presents, then swap first and last numbers.
else:
i = 0
while i < size // 2:
# swap present and preceding numbers at time and jump to second element after swap
arr[i], arr[size - i - 1] = arr[size - i - 1], arr[i]
# skip if present and preceding numbers indexes are same
if (i != i + 1 and size - i - 1 != size - i - 2) and (
i != size - i - 2 and size - i - 1 != i + 1
):
arr[i + 1], arr[size - i - 2] = arr[size - i - 2], arr[i + 1]
i += 2
return arr
arr = [1, 2, 3, 4, 5]
size = 5
print("Original list: ", arr)
print("Reversed list: ", list_reverse(arr, size))
# This contributed by SR.Dhanush
Input: arr[], size
1)if length of array is 1, then return arr.
[END] |
Python | Reversing a List | https://www.geeksforgeeks.org/python-reversing-list/ | lst = [10, 11, 12, 13, 14, 15]
lst.reverse()
print("Using reverse() ", lst)
print("Using reversed() ", list(reversed(lst))) | Input: arr[], size
1)if length of array is 1, then return arr. | Python | Reversing a List
lst = [10, 11, 12, 13, 14, 15]
lst.reverse()
print("Using reverse() ", lst)
print("Using reversed() ", list(reversed(lst)))
Input: arr[], size
1)if length of array is 1, then return arr.
[END] |
Python | Reversing a List | https://www.geeksforgeeks.org/python-reversing-list/ | # Reversing a list using two-pointer approach
def reverse_list(arr):
left = 0
right = len(arr) - 1
while left < right:
# Swap
temp = arr[left]
arr[left] = arr[right]
arr[right] = temp
left += 1
right -= 1
return arr
arr = [1, 2, 3, 4, 5, 6, 7]
print(reverse_list(arr)) | Input: arr[], size
1)if length of array is 1, then return arr. | Python | Reversing a List
# Reversing a list using two-pointer approach
def reverse_list(arr):
left = 0
right = len(arr) - 1
while left < right:
# Swap
temp = arr[left]
arr[left] = arr[right]
arr[right] = temp
left += 1
right -= 1
return arr
arr = [1, 2, 3, 4, 5, 6, 7]
print(reverse_list(arr))
Input: arr[], size
1)if length of array is 1, then return arr.
[END] |
Python | Reversing a List | https://www.geeksforgeeks.org/python-reversing-list/ | # input list
lst = [10, 11, 12, 13, 14, 15]
# the above input can also be given as
# lst=list(map(int,input().split()))
l = [] # empty list
# iterate to reverse the list
for i in lst:
# reversing the list
l.insert(0, i)
# printing result
print(l) | Input: arr[], size
1)if length of array is 1, then return arr. | Python | Reversing a List
# input list
lst = [10, 11, 12, 13, 14, 15]
# the above input can also be given as
# lst=list(map(int,input().split()))
l = [] # empty list
# iterate to reverse the list
for i in lst:
# reversing the list
l.insert(0, i)
# printing result
print(l)
Input: arr[], size
1)if length of array is 1, then return arr.
[END] |
Python | Reversing a List | https://www.geeksforgeeks.org/python-reversing-list/ | # Reversing a list using slicing technique
def Reverse(lst):
new_lst = lst[::-1]
return new_lst
lst = [10, 11, 12, 13, 14, 15]
print(Reverse(lst)) | Input: arr[], size
1)if length of array is 1, then return arr. | Python | Reversing a List
# Reversing a list using slicing technique
def Reverse(lst):
new_lst = lst[::-1]
return new_lst
lst = [10, 11, 12, 13, 14, 15]
print(Reverse(lst))
Input: arr[], size
1)if length of array is 1, then return arr.
[END] |
Python | Reversing a List | https://www.geeksforgeeks.org/python-reversing-list/ | original_list = [10, 11, 12, 13, 14, 15]
new_list = [
original_list[len(original_list) - i] for i in range(1, len(original_list) + 1)
]
print(new_list) | Input: arr[], size
1)if length of array is 1, then return arr. | Python | Reversing a List
original_list = [10, 11, 12, 13, 14, 15]
new_list = [
original_list[len(original_list) - i] for i in range(1, len(original_list) + 1)
]
print(new_list)
Input: arr[], size
1)if length of array is 1, then return arr.
[END] |
Python | Reversing a List | https://www.geeksforgeeks.org/python-reversing-list/ | from functools import reduce
original_list = [10, 11, 12, 13, 14, 15]
new_list = reduce(lambda a, b: [b] + [a] if type(a) == int else [b] + a, original_list)
print(new_list) | Input: arr[], size
1)if length of array is 1, then return arr. | Python | Reversing a List
from functools import reduce
original_list = [10, 11, 12, 13, 14, 15]
new_list = reduce(lambda a, b: [b] + [a] if type(a) == int else [b] + a, original_list)
print(new_list)
Input: arr[], size
1)if length of array is 1, then return arr.
[END] |
Python | Reversing a List | https://www.geeksforgeeks.org/python-reversing-list/ | def reverse_list(lst):
reversed_lst = []
for i in range(len(lst) - 1, -1, -1):
reversed_lst.append(lst[i])
return reversed_lst
lst = [1, 2, 3, 4, 5]
reversed_lst = reverse_list(lst)
print(reversed_lst) # Output: [5, 4, 3, 2, 1] | Input: arr[], size
1)if length of array is 1, then return arr. | Python | Reversing a List
def reverse_list(lst):
reversed_lst = []
for i in range(len(lst) - 1, -1, -1):
reversed_lst.append(lst[i])
return reversed_lst
lst = [1, 2, 3, 4, 5]
reversed_lst = reverse_list(lst)
print(reversed_lst) # Output: [5, 4, 3, 2, 1]
Input: arr[], size
1)if length of array is 1, then return arr.
[END] |
Python | Reversing a List | https://www.geeksforgeeks.org/python-reversing-list/ | # Input list
my_list = [4, 5, 6, 7, 8, 9]
# Initialize an empty list to store reversed values
reversed_list = []
# Loop through the original list backwards
for i in range(len(my_list) - 1, -1, -1):
# Insert each value at the end of the reversed list
reversed_list.append(my_list[i])
# Print the reversed list
print(reversed_list) | Input: arr[], size
1)if length of array is 1, then return arr. | Python | Reversing a List
# Input list
my_list = [4, 5, 6, 7, 8, 9]
# Initialize an empty list to store reversed values
reversed_list = []
# Loop through the original list backwards
for i in range(len(my_list) - 1, -1, -1):
# Insert each value at the end of the reversed list
reversed_list.append(my_list[i])
# Print the reversed list
print(reversed_list)
Input: arr[], size
1)if length of array is 1, then return arr.
[END] |
Python | Reversing a List | https://www.geeksforgeeks.org/python-reversing-list/ | import numpy as np
# Input list
my_list = [4, 5, 6, 7, 8, 9]
# Convert the list to a 1D numpy array
my_array = np.array(my_list)
# Reverse the order of the array
reversed_array = my_array[::-1]
# Convert the reversed array to a list
reversed_list = reversed_array.tolist()
# Print the reversed list
print(reversed_list)
# This code is contributed by Jyothi pinjala. | Input: arr[], size
1)if length of array is 1, then return arr. | Python | Reversing a List
import numpy as np
# Input list
my_list = [4, 5, 6, 7, 8, 9]
# Convert the list to a 1D numpy array
my_array = np.array(my_list)
# Reverse the order of the array
reversed_array = my_array[::-1]
# Convert the reversed array to a list
reversed_list = reversed_array.tolist()
# Print the reversed list
print(reversed_list)
# This code is contributed by Jyothi pinjala.
Input: arr[], size
1)if length of array is 1, then return arr.
[END] |
Python | Cloning or Copying a list | https://www.geeksforgeeks.org/python-cloning-copying-list/ | # Python program to copy or clone a list
# Using the Slice Operator
def Cloning(li1):
li_copy = li1[:]
return li_copy
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2) |
#Output : Original List: [4, 8, 2, 10, 15, 18]?? | Python | Cloning or Copying a list
# Python program to copy or clone a list
# Using the Slice Operator
def Cloning(li1):
li_copy = li1[:]
return li_copy
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)
#Output : Original List: [4, 8, 2, 10, 15, 18]??
[END] |
Python | Cloning or Copying a list | https://www.geeksforgeeks.org/python-cloning-copying-list/ | # Python code to clone or copy a list
# Using the in-built function extend()
def Cloning(li1):
li_copy = []
li_copy.extend(li1)
return li_copy
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2) |
#Output : Original List: [4, 8, 2, 10, 15, 18]?? | Python | Cloning or Copying a list
# Python code to clone or copy a list
# Using the in-built function extend()
def Cloning(li1):
li_copy = []
li_copy.extend(li1)
return li_copy
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)
#Output : Original List: [4, 8, 2, 10, 15, 18]??
[END] |
Python | Cloning or Copying a list | https://www.geeksforgeeks.org/python-cloning-copying-list/ | # Python code to clone or copy a list
# Using the List copy using =
def Cloning(li1):
li_copy = li1
return li_copy
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2) |
#Output : Original List: [4, 8, 2, 10, 15, 18]?? | Python | Cloning or Copying a list
# Python code to clone or copy a list
# Using the List copy using =
def Cloning(li1):
li_copy = li1
return li_copy
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)
#Output : Original List: [4, 8, 2, 10, 15, 18]??
[END] |
Python | Cloning or Copying a list | https://www.geeksforgeeks.org/python-cloning-copying-list/ | # importing copy module
import copy
# initializing list 1
li1 = [1, 2, [3, 5], 4]
# using copy for shallow copy
li2 = copy.copy(li1)
print(li2) |
#Output : Original List: [4, 8, 2, 10, 15, 18]?? | Python | Cloning or Copying a list
# importing copy module
import copy
# initializing list 1
li1 = [1, 2, [3, 5], 4]
# using copy for shallow copy
li2 = copy.copy(li1)
print(li2)
#Output : Original List: [4, 8, 2, 10, 15, 18]??
[END] |
Python | Cloning or Copying a list | https://www.geeksforgeeks.org/python-cloning-copying-list/ | # Python code to clone or copy a list
# Using list comprehension
def Cloning(li1):
li_copy = [i for i in li1]
return li_copy
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2) |
#Output : Original List: [4, 8, 2, 10, 15, 18]?? | Python | Cloning or Copying a list
# Python code to clone or copy a list
# Using list comprehension
def Cloning(li1):
li_copy = [i for i in li1]
return li_copy
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)
#Output : Original List: [4, 8, 2, 10, 15, 18]??
[END] |
Python | Cloning or Copying a list | https://www.geeksforgeeks.org/python-cloning-copying-list/ | # Python code to clone or copy a list
# Using append()
def Cloning(li1):
li_copy = []
for item in li1:
li_copy.append(item)
return li_copy
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2) |
#Output : Original List: [4, 8, 2, 10, 15, 18]?? | Python | Cloning or Copying a list
# Python code to clone or copy a list
# Using append()
def Cloning(li1):
li_copy = []
for item in li1:
li_copy.append(item)
return li_copy
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)
#Output : Original List: [4, 8, 2, 10, 15, 18]??
[END] |
Python | Cloning or Copying a list | https://www.geeksforgeeks.org/python-cloning-copying-list/ | # Python code to clone or copy a list
# Using built-in method copy()
def Cloning(li1):
li_copy = []
li_copy = li1.copy()
return li_copy
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2) |
#Output : Original List: [4, 8, 2, 10, 15, 18]?? | Python | Cloning or Copying a list
# Python code to clone or copy a list
# Using built-in method copy()
def Cloning(li1):
li_copy = []
li_copy = li1.copy()
return li_copy
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)
#Output : Original List: [4, 8, 2, 10, 15, 18]??
[END] |
Python | Cloning or Copying a list | https://www.geeksforgeeks.org/python-cloning-copying-list/ | # importing copy module
import copy
# initializing list 1
li1 = [1, 2, [3, 5], 4]
# using deepcopy for deepcopy
li3 = copy.deepcopy(li1)
print(li3) |
#Output : Original List: [4, 8, 2, 10, 15, 18]?? | Python | Cloning or Copying a list
# importing copy module
import copy
# initializing list 1
li1 = [1, 2, [3, 5], 4]
# using deepcopy for deepcopy
li3 = copy.deepcopy(li1)
print(li3)
#Output : Original List: [4, 8, 2, 10, 15, 18]??
[END] |
Python | Cloning or Copying a list | https://www.geeksforgeeks.org/python-cloning-copying-list/ | # Python code to clone or copy a list
# Using list comprehension
lst = [4, 8, 2, 10, 15, 18]
li_copy = [i for a, i in enumerate(lst)]
print("Original List:", lst)
print("After Cloning:", li_copy) |
#Output : Original List: [4, 8, 2, 10, 15, 18]?? | Python | Cloning or Copying a list
# Python code to clone or copy a list
# Using list comprehension
lst = [4, 8, 2, 10, 15, 18]
li_copy = [i for a, i in enumerate(lst)]
print("Original List:", lst)
print("After Cloning:", li_copy)
#Output : Original List: [4, 8, 2, 10, 15, 18]??
[END] |
Python | Cloning or Copying a list | https://www.geeksforgeeks.org/python-cloning-copying-list/ | # Python code to clone or copy a list
# Using map function
lst = [4, 8, 2, 10, 15, 18]
li_copy = map(int, lst)
print("Original List:", lst)
print("After Cloning:", *li_copy) |
#Output : Original List: [4, 8, 2, 10, 15, 18]?? | Python | Cloning or Copying a list
# Python code to clone or copy a list
# Using map function
lst = [4, 8, 2, 10, 15, 18]
li_copy = map(int, lst)
print("Original List:", lst)
print("After Cloning:", *li_copy)
#Output : Original List: [4, 8, 2, 10, 15, 18]??
[END] |
Python | Cloning or Copying a list | https://www.geeksforgeeks.org/python-cloning-copying-list/ | # Python code to clone or copy a list
# Using slice() function
lst = [4, 8, 2, 10, 15, 18]
li_copy = lst[slice(len(lst))]
print("Original List:", lst)
print("After Cloning:", li_copy)
# This code is contributed by Pushpesh Raj. |
#Output : Original List: [4, 8, 2, 10, 15, 18]?? | Python | Cloning or Copying a list
# Python code to clone or copy a list
# Using slice() function
lst = [4, 8, 2, 10, 15, 18]
li_copy = lst[slice(len(lst))]
print("Original List:", lst)
print("After Cloning:", li_copy)
# This code is contributed by Pushpesh Raj.
#Output : Original List: [4, 8, 2, 10, 15, 18]??
[END] |
Python | Cloning or Copying a list | https://www.geeksforgeeks.org/python-cloning-copying-list/ | from collections import deque
original_list = [4, 8, 2, 10, 15, 18]
copied_list = deque(original_list)
copied_list = list(copied_list)
print("Original List:", original_list)
print("After Cloning:", copied_list) |
#Output : Original List: [4, 8, 2, 10, 15, 18]?? | Python | Cloning or Copying a list
from collections import deque
original_list = [4, 8, 2, 10, 15, 18]
copied_list = deque(original_list)
copied_list = list(copied_list)
print("Original List:", original_list)
print("After Cloning:", copied_list)
#Output : Original List: [4, 8, 2, 10, 15, 18]??
[END] |
Python | Cloning or Copying a list | https://www.geeksforgeeks.org/python-cloning-copying-list/ | from functools import reduce
def clone_list(li1):
return reduce(lambda x, y: x + [y], li1, [])
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = clone_list(li1)
print("Original List:", li1)
print("After Cloning:", li2)
# This code is contributed by Jyothi pinjala. |
#Output : Original List: [4, 8, 2, 10, 15, 18]?? | Python | Cloning or Copying a list
from functools import reduce
def clone_list(li1):
return reduce(lambda x, y: x + [y], li1, [])
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = clone_list(li1)
print("Original List:", li1)
print("After Cloning:", li2)
# This code is contributed by Jyothi pinjala.
#Output : Original List: [4, 8, 2, 10, 15, 18]??
[END] |
Python | Count occurrences of an element in a list | https://www.geeksforgeeks.org/python-count-occurrences-element-list/ | # Python code to count the number of occurrences
def countX(lst, x):
count = 0
for ele in lst:
if ele == x:
count = count + 1
return count
# Driver Code
lst = [8, 6, 8, 10, 8, 20, 10, 8, 8]
x = 8
print("{} has occurred {} times".format(x, countX(lst, x))) | Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3 | Python | Count occurrences of an element in a list
# Python code to count the number of occurrences
def countX(lst, x):
count = 0
for ele in lst:
if ele == x:
count = count + 1
return count
# Driver Code
lst = [8, 6, 8, 10, 8, 20, 10, 8, 8]
x = 8
print("{} has occurred {} times".format(x, countX(lst, x)))
Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3
[END] |
Python | Count occurrences of an element in a list | https://www.geeksforgeeks.org/python-count-occurrences-element-list/ | # Python code to count the number of occurrences
def countX(lst, x):
return lst.count(x)
# Driver Code
lst = [8, 6, 8, 10, 8, 20, 10, 8, 8]
x = 8
print("{} has occurred {} times".format(x, countX(lst, x))) | Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3 | Python | Count occurrences of an element in a list
# Python code to count the number of occurrences
def countX(lst, x):
return lst.count(x)
# Driver Code
lst = [8, 6, 8, 10, 8, 20, 10, 8, 8]
x = 8
print("{} has occurred {} times".format(x, countX(lst, x)))
Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3
[END] |
Python | Count occurrences of an element in a list | https://www.geeksforgeeks.org/python-count-occurrences-element-list/ | from collections import Counter
# declaring the list
l = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
# driver program
x = 3
d = Counter(l)
print("{} has occurred {} times".format(x, d[x])) | Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3 | Python | Count occurrences of an element in a list
from collections import Counter
# declaring the list
l = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
# driver program
x = 3
d = Counter(l)
print("{} has occurred {} times".format(x, d[x]))
Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3
[END] |
Python | Count occurrences of an element in a list | https://www.geeksforgeeks.org/python-count-occurrences-element-list/ | import operator as op
# declaring the list
l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5]
# driver program
x = 2
print(f"{x} has occurred {op.countOf(l, x)} times") | Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3 | Python | Count occurrences of an element in a list
import operator as op
# declaring the list
l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5]
# driver program
x = 2
print(f"{x} has occurred {op.countOf(l, x)} times")
Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3
[END] |
Python | Count occurrences of an element in a list | https://www.geeksforgeeks.org/python-count-occurrences-element-list/ | lis = ["a", "d", "d", "c", "a", "b", "b", "a", "c", "d", "e"]
occurrence = {item: lis.count(item) for item in lis}
print(occurrence.get("e")) | Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3 | Python | Count occurrences of an element in a list
lis = ["a", "d", "d", "c", "a", "b", "b", "a", "c", "d", "e"]
occurrence = {item: lis.count(item) for item in lis}
print(occurrence.get("e"))
Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3
[END] |
Python | Count occurrences of an element in a list | https://www.geeksforgeeks.org/python-count-occurrences-element-list/ | import pandas as pd
# declaring the list
l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5]
count = pd.Series(l).value_counts()
print("Element Count")
print(count) | Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3 | Python | Count occurrences of an element in a list
import pandas as pd
# declaring the list
l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5]
count = pd.Series(l).value_counts()
print("Element Count")
print(count)
Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3
[END] |
Python | Count occurrences of an element in a list | https://www.geeksforgeeks.org/python-count-occurrences-element-list/ | l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5]
ele = 1
x = [i for i in l if i == ele]
print("the element", ele, "occurs", len(x), "times") | Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3 | Python | Count occurrences of an element in a list
l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5]
ele = 1
x = [i for i in l if i == ele]
print("the element", ele, "occurs", len(x), "times")
Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3
[END] |
Python | Count occurrences of an element in a list | https://www.geeksforgeeks.org/python-count-occurrences-element-list/ | l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5]
ele = 1
x = [i for j, i in enumerate(l) if i == ele]
print("the element", ele, "occurs", len(x), "times") | Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3 | Python | Count occurrences of an element in a list
l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5]
ele = 1
x = [i for j, i in enumerate(l) if i == ele]
print("the element", ele, "occurs", len(x), "times")
Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10
Output: 3
[END] |
Python Program to find sum and average of List in Python | https://www.geeksforgeeks.org/find-sum-and-average-of-list-in-python/ | # Python program to find the sum
# and average of the list
L = [4, 5, 1, 2, 9, 7, 10, 8]
# variable to store the sum of
# the list
count = 0
# Finding the sum
for i in L:
count += i
# divide the total elements by
# number of elements
avg = count / len(L)
print("sum = ", count)
print("average = ", avg) | Input: [4, 5, 1, 2, 9, 7, 10, 8]
Output: | Python Program to find sum and average of List in Python
# Python program to find the sum
# and average of the list
L = [4, 5, 1, 2, 9, 7, 10, 8]
# variable to store the sum of
# the list
count = 0
# Finding the sum
for i in L:
count += i
# divide the total elements by
# number of elements
avg = count / len(L)
print("sum = ", count)
print("average = ", avg)
Input: [4, 5, 1, 2, 9, 7, 10, 8]
Output:
[END] |
Python Program to find sum and average of List in Python | https://www.geeksforgeeks.org/find-sum-and-average-of-list-in-python/ | # Python program to find the sum
# and average of the list
L = [4, 5, 1, 2, 9, 7, 10, 8]
# using sum() method
count = sum(L)
# finding average
avg = count / len(L)
print("sum = ", count)
print("average = ", avg) | Input: [4, 5, 1, 2, 9, 7, 10, 8]
Output: | Python Program to find sum and average of List in Python
# Python program to find the sum
# and average of the list
L = [4, 5, 1, 2, 9, 7, 10, 8]
# using sum() method
count = sum(L)
# finding average
avg = count / len(L)
print("sum = ", count)
print("average = ", avg)
Input: [4, 5, 1, 2, 9, 7, 10, 8]
Output:
[END] |
Python Program to find sum and average of List in Python | https://www.geeksforgeeks.org/find-sum-and-average-of-list-in-python/ | # Python program to find the sum
# and average of the list
import statistics
L = [4, 5, 1, 2, 9, 7, 10, 8]
# using sum() method
sum1 = sum(L)
# finding average
avg = statistics.mean(L)
print("sum = ", sum1)
print("average = ", avg) | Input: [4, 5, 1, 2, 9, 7, 10, 8]
Output: | Python Program to find sum and average of List in Python
# Python program to find the sum
# and average of the list
import statistics
L = [4, 5, 1, 2, 9, 7, 10, 8]
# using sum() method
sum1 = sum(L)
# finding average
avg = statistics.mean(L)
print("sum = ", sum1)
print("average = ", avg)
Input: [4, 5, 1, 2, 9, 7, 10, 8]
Output:
[END] |
Python Program to find sum and average of List in Python | https://www.geeksforgeeks.org/find-sum-and-average-of-list-in-python/ | import numpy as np
L = [4, 5, 1, 2, 9, 7, 10, 8]
# finding sum of list using numpy
sum_ = np.sum(L)
# finding average of list using numpy
avg = np.average(L)
print("sum = ", sum_)
print("average = ", avg) | Input: [4, 5, 1, 2, 9, 7, 10, 8]
Output: | Python Program to find sum and average of List in Python
import numpy as np
L = [4, 5, 1, 2, 9, 7, 10, 8]
# finding sum of list using numpy
sum_ = np.sum(L)
# finding average of list using numpy
avg = np.average(L)
print("sum = ", sum_)
print("average = ", avg)
Input: [4, 5, 1, 2, 9, 7, 10, 8]
Output:
[END] |
Python Program to find sum and average of List in Python | https://www.geeksforgeeks.org/find-sum-and-average-of-list-in-python/ | def sum_avg_list(lst, n):
if n == 0:
return (0, 0)
else:
sum, avg = sum_avg_list(lst, n - 1)
return (sum + lst[n - 1], avg + 1)
def avg_list(lst):
sum, avg = sum_avg_list(lst, len(lst))
return sum / avg
lst = [4, 5, 1, 2, 9, 7, 10, 8]
print("Sum of the list: ", sum_avg_list(lst, len(lst))[0])
print("Average of the list: ", avg_list(lst))
lst = [15, 9, 55, 41, 35, 20, 62, 49]
print("Sum of the list: ", sum_avg_list(lst, len(lst))[0])
print("Average of the list: ", avg_list(lst)) | Input: [4, 5, 1, 2, 9, 7, 10, 8]
Output: | Python Program to find sum and average of List in Python
def sum_avg_list(lst, n):
if n == 0:
return (0, 0)
else:
sum, avg = sum_avg_list(lst, n - 1)
return (sum + lst[n - 1], avg + 1)
def avg_list(lst):
sum, avg = sum_avg_list(lst, len(lst))
return sum / avg
lst = [4, 5, 1, 2, 9, 7, 10, 8]
print("Sum of the list: ", sum_avg_list(lst, len(lst))[0])
print("Average of the list: ", avg_list(lst))
lst = [15, 9, 55, 41, 35, 20, 62, 49]
print("Sum of the list: ", sum_avg_list(lst, len(lst))[0])
print("Average of the list: ", avg_list(lst))
Input: [4, 5, 1, 2, 9, 7, 10, 8]
Output:
[END] |
Python Program to find sum and average of List in Python | https://www.geeksforgeeks.org/find-sum-and-average-of-list-in-python/ | from functools import reduce
L = [4, 5, 1, 2, 9, 7, 10, 8]
# Finding the sum using reduce() and lambda
count = reduce(lambda x, y: x + y, L)
# divide the total elements by number of elements
avg = count / len(L)
print("sum = ", count)
print("average = ", avg) | Input: [4, 5, 1, 2, 9, 7, 10, 8]
Output: | Python Program to find sum and average of List in Python
from functools import reduce
L = [4, 5, 1, 2, 9, 7, 10, 8]
# Finding the sum using reduce() and lambda
count = reduce(lambda x, y: x + y, L)
# divide the total elements by number of elements
avg = count / len(L)
print("sum = ", count)
print("average = ", avg)
Input: [4, 5, 1, 2, 9, 7, 10, 8]
Output:
[END] |
Python Program to find sum and average of List in Python | https://www.geeksforgeeks.org/find-sum-and-average-of-list-in-python/ | L = [4, 5, 1, 2, 9, 7, 10, 8]
# Finding the sum
count = sum(L)
# Finding the average
avg = count / len(L)
print("sum = ", count)
print("average = ", avg) | Input: [4, 5, 1, 2, 9, 7, 10, 8]
Output: | Python Program to find sum and average of List in Python
L = [4, 5, 1, 2, 9, 7, 10, 8]
# Finding the sum
count = sum(L)
# Finding the average
avg = count / len(L)
print("sum = ", count)
print("average = ", avg)
Input: [4, 5, 1, 2, 9, 7, 10, 8]
Output:
[END] |
Python | Sum of number digits in List | https://www.geeksforgeeks.org/python-sum-of-number-digits-in-list/?ref=leftbar-rightbar | # Python3 code to demonstrate
# Sum of number digits in List
# using loop + str()
# Initializing list
test_list = [12, 67, 98, 34]
# printing original list
print("The original list is : " + str(test_list))
# Sum of number digits in List
# using loop + str()
res = []
for ele in test_list:
sum = 0
for digit in str(ele):
sum += int(digit)
res.append(sum)
# printing result
print("List Integer Summation : " + str(res)) |
#Output : The original list is : [12, 67, 98, 34] | Python | Sum of number digits in List
# Python3 code to demonstrate
# Sum of number digits in List
# using loop + str()
# Initializing list
test_list = [12, 67, 98, 34]
# printing original list
print("The original list is : " + str(test_list))
# Sum of number digits in List
# using loop + str()
res = []
for ele in test_list:
sum = 0
for digit in str(ele):
sum += int(digit)
res.append(sum)
# printing result
print("List Integer Summation : " + str(res))
#Output : The original list is : [12, 67, 98, 34]
[END] |
Python | Sum of number digits in List | https://www.geeksforgeeks.org/python-sum-of-number-digits-in-list/?ref=leftbar-rightbar | # Python3 code to demonstrate
# Sum of number digits in List
# using sum() + list comprehension
# Initializing list
test_list = [12, 67, 98, 34]
# printing original list
print("The original list is : " + str(test_list))
# Sum of number digits in List
# using sum() + list comprehension
res = list(map(lambda ele: sum(int(sub) for sub in str(ele)), test_list))
# printing result
print("List Integer Summation : " + str(res)) |
#Output : The original list is : [12, 67, 98, 34] | Python | Sum of number digits in List
# Python3 code to demonstrate
# Sum of number digits in List
# using sum() + list comprehension
# Initializing list
test_list = [12, 67, 98, 34]
# printing original list
print("The original list is : " + str(test_list))
# Sum of number digits in List
# using sum() + list comprehension
res = list(map(lambda ele: sum(int(sub) for sub in str(ele)), test_list))
# printing result
print("List Integer Summation : " + str(res))
#Output : The original list is : [12, 67, 98, 34]
[END] |
Python | Sum of number digits in List | https://www.geeksforgeeks.org/python-sum-of-number-digits-in-list/?ref=leftbar-rightbar | # Python3 code to demonstrate
# Sum of number digits in a List
# using sum() + reduce()
from functools import reduce
# Initializing list
test_list = [12, 67, 98, 34]
# printing original list
print("The original list is : " + str(test_list))
# Sum of number digits in List
# using sum() + reduce()
res = [reduce(lambda x, y: int(x) + int(y), list(str(i))) for i in test_list]
# printing result
print("List Integer Summation : " + str(res)) |
#Output : The original list is : [12, 67, 98, 34] | Python | Sum of number digits in List
# Python3 code to demonstrate
# Sum of number digits in a List
# using sum() + reduce()
from functools import reduce
# Initializing list
test_list = [12, 67, 98, 34]
# printing original list
print("The original list is : " + str(test_list))
# Sum of number digits in List
# using sum() + reduce()
res = [reduce(lambda x, y: int(x) + int(y), list(str(i))) for i in test_list]
# printing result
print("List Integer Summation : " + str(res))
#Output : The original list is : [12, 67, 98, 34]
[END] |
Python | Sum of number digits in List | https://www.geeksforgeeks.org/python-sum-of-number-digits-in-list/?ref=leftbar-rightbar | import numpy as np
# Initializing list
test_list = [12, 67, 98, 34]
# printing original list
print("The original list is : " + str(test_list))
# Sum of number digits in List
# using numpy
res = np.sum([list(map(int, str(ele))) for ele in test_list], axis=1)
# printing result
print("List Integer Summation : " + str(list(res))) |
#Output : The original list is : [12, 67, 98, 34] | Python | Sum of number digits in List
import numpy as np
# Initializing list
test_list = [12, 67, 98, 34]
# printing original list
print("The original list is : " + str(test_list))
# Sum of number digits in List
# using numpy
res = np.sum([list(map(int, str(ele))) for ele in test_list], axis=1)
# printing result
print("List Integer Summation : " + str(list(res)))
#Output : The original list is : [12, 67, 98, 34]
[END] |
Python | Sum of number digits in List | https://www.geeksforgeeks.org/python-sum-of-number-digits-in-list/?ref=leftbar-rightbar | # Python3 code to demonstrate
# Sum of number digits in List
# using itertools library
# importing itertools library
import itertools
# Initializing list
test_list = [12, 67, 98, 34]
# printing original list
print("The original list is : " + str(test_list))
# Sum of number digits in List
# using itertools library
res = [sum(map(int, list(itertools.chain(*str(ele))))) for ele in test_list]
# printing result
print("List Integer Summation : " + str(res))
# This code is contributed by Jyothi pinjala. |
#Output : The original list is : [12, 67, 98, 34] | Python | Sum of number digits in List
# Python3 code to demonstrate
# Sum of number digits in List
# using itertools library
# importing itertools library
import itertools
# Initializing list
test_list = [12, 67, 98, 34]
# printing original list
print("The original list is : " + str(test_list))
# Sum of number digits in List
# using itertools library
res = [sum(map(int, list(itertools.chain(*str(ele))))) for ele in test_list]
# printing result
print("List Integer Summation : " + str(res))
# This code is contributed by Jyothi pinjala.
#Output : The original list is : [12, 67, 98, 34]
[END] |
Python | Sum of number digits in List | https://www.geeksforgeeks.org/python-sum-of-number-digits-in-list/?ref=leftbar-rightbar | lst = [12, 67, 98, 34]
def digit_sum(num):
digit_sum = 0
while num > 0:
digit_sum += num % 10
num //= 10
return digit_sum
def sum_of_digits_list(lst):
return list(map(digit_sum, lst))
print(sum_of_digits_list(lst)) |
#Output : The original list is : [12, 67, 98, 34] | Python | Sum of number digits in List
lst = [12, 67, 98, 34]
def digit_sum(num):
digit_sum = 0
while num > 0:
digit_sum += num % 10
num //= 10
return digit_sum
def sum_of_digits_list(lst):
return list(map(digit_sum, lst))
print(sum_of_digits_list(lst))
#Output : The original list is : [12, 67, 98, 34]
[END] |
Python | Sum of number digits in List | https://www.geeksforgeeks.org/python-sum-of-number-digits-in-list/?ref=leftbar-rightbar | # Initializing list
test_list = [12, 67, 98, 34]
# printing original list
print("The original list is : " + str(test_list))
# Sum of number digits in List
# creating an expression
res = list(sum(int(digit) for digit in str(num)) for num in test_list)
# printing result
print("List Integer Summation : " + str(list(res))) |
#Output : The original list is : [12, 67, 98, 34] | Python | Sum of number digits in List
# Initializing list
test_list = [12, 67, 98, 34]
# printing original list
print("The original list is : " + str(test_list))
# Sum of number digits in List
# creating an expression
res = list(sum(int(digit) for digit in str(num)) for num in test_list)
# printing result
print("List Integer Summation : " + str(list(res)))
#Output : The original list is : [12, 67, 98, 34]
[END] |
Python | Multiply all numbers in the list | https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/ | # Python program to multiply all values in the
# list using traversal
def multiplyList(myList):
# Multiply elements one by one
result = 1
for x in myList:
result = result * x
return result
# Driver code
list1 = [1, 2, 3]
list2 = [3, 2, 4]
print(multiplyList(list1))
print(multiplyList(list2)) |
#Output : 6 | Python | Multiply all numbers in the list
# Python program to multiply all values in the
# list using traversal
def multiplyList(myList):
# Multiply elements one by one
result = 1
for x in myList:
result = result * x
return result
# Driver code
list1 = [1, 2, 3]
list2 = [3, 2, 4]
print(multiplyList(list1))
print(multiplyList(list2))
#Output : 6
[END] |
Python | Multiply all numbers in the list | https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/ | # Python3 program to multiply all values in the
# list using numpy.prod()
import numpy
list1 = [1, 2, 3]
list2 = [3, 2, 4]
# using numpy.prod() to get the multiplications
result1 = numpy.prod(list1)
result2 = numpy.prod(list2)
print(result1)
print(result2) |
#Output : 6 | Python | Multiply all numbers in the list
# Python3 program to multiply all values in the
# list using numpy.prod()
import numpy
list1 = [1, 2, 3]
list2 = [3, 2, 4]
# using numpy.prod() to get the multiplications
result1 = numpy.prod(list1)
result2 = numpy.prod(list2)
print(result1)
print(result2)
#Output : 6
[END] |
Python | Multiply all numbers in the list | https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/ | # Python3 program to multiply all values in the
# list using lambda function and reduce()
from functools import reduce
list1 = [1, 2, 3]
list2 = [3, 2, 4]
result1 = reduce((lambda x, y: x * y), list1)
result2 = reduce((lambda x, y: x * y), list2)
print(result1)
print(result2) |
#Output : 6 | Python | Multiply all numbers in the list
# Python3 program to multiply all values in the
# list using lambda function and reduce()
from functools import reduce
list1 = [1, 2, 3]
list2 = [3, 2, 4]
result1 = reduce((lambda x, y: x * y), list1)
result2 = reduce((lambda x, y: x * y), list2)
print(result1)
print(result2)
#Output : 6
[END] |
Python | Multiply all numbers in the list | https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/ | # Python3 program to multiply all values in the
# list using math.prod
import math
list1 = [1, 2, 3]
list2 = [3, 2, 4]
result1 = math.prod(list1)
result2 = math.prod(list2)
print(result1)
print(result2) |
#Output : 6 | Python | Multiply all numbers in the list
# Python3 program to multiply all values in the
# list using math.prod
import math
list1 = [1, 2, 3]
list2 = [3, 2, 4]
result1 = math.prod(list1)
result2 = math.prod(list2)
print(result1)
print(result2)
#Output : 6
[END] |
Python | Multiply all numbers in the list | https://www.geeksforgeeks.org/python-multiply-numbers-list-3-different-ways/ | # Python 3 program to multiply all numbers in
# the given list by importing operator module
from operator import *
list1 = [1, 2, 3]
m = 1
for i in list1:
# multiplying all elements in the given list
# using mul function of operator module
m = mul(i, m)
# printing the result
print(m) |
#Output : 6 | Python | Multiply all numbers in the list
# Python 3 program to multiply all numbers in
# the given list by importing operator module
from operator import *
list1 = [1, 2, 3]
m = 1
for i in list1:
# multiplying all elements in the given list
# using mul function of operator module
m = mul(i, m)
# printing the result
print(m)
#Output : 6
[END] |
End of preview. Expand
in Dataset Viewer.
No dataset card yet
New: Create and edit this dataset card directly on the website!
Contribute a Dataset Card- Downloads last month
- 0