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 count Even and Odd numbers in a List | https://www.geeksforgeeks.org/python-program-to-count-even-and-odd-numbers-in-a-list/ | # Python program to count Even
# and Odd numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
even_count, odd_count = 0, 0
# Using sum function
odd_count = sum(1 for i in list1 if i & 1)
even_count = len(list1) - odd_count
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count) |
#Output : Even numbers in the list: 3 | Python program to count Even and Odd numbers in a List
# Python program to count Even
# and Odd numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
even_count, odd_count = 0, 0
# Using sum function
odd_count = sum(1 for i in list1 if i & 1)
even_count = len(list1) - odd_count
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
#Output : Even numbers in the list: 3
[END] |
Python program to print positive numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/ | # Python program to print positive Numbers in a List??????# list of numberslist1 = [11, -21, 0, 45, 66, -93]??????# iterating each number in listfor num in list1:??????????????????????????????# checking condition????????????????????????if num & gt????????????????????????= 0:???????????????????????????????????????? | Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64 | Python program to print positive numbers in a list
# Python program to print positive Numbers in a List??????# list of numberslist1 = [11, -21, 0, 45, 66, -93]??????# iterating each number in listfor num in list1:??????????????????????????????# checking condition????????????????????????if num & gt????????????????????????= 0:????????????????????????????????????????
Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64
[END] |
Python program to print positive numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/ | # Python program to print positive Numbers in a List
# list of numbers
list1 = [-10, 21, -4, -45, -66, 93]
num = 0
# using while loop
while num < len(list1):
# checking condition
if list1[num] >= 0:
print(list1[num], end=" ")
# increment num
num += 1 | Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64 | Python program to print positive numbers in a list
# Python program to print positive Numbers in a List
# list of numbers
list1 = [-10, 21, -4, -45, -66, 93]
num = 0
# using while loop
while num < len(list1):
# checking condition
if list1[num] >= 0:
print(list1[num], end=" ")
# increment num
num += 1
Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64
[END] |
Python program to print positive numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/ | # Python program to print Positive Numbers in a List
# list of numbers
list1 = [-10, -21, -4, 45, -66, 93]
# using list comprehension
pos_nos = [num for num in list1 if num >= 0]
print("Positive numbers in the list: ", *pos_nos) | Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64 | Python program to print positive numbers in a list
# Python program to print Positive Numbers in a List
# list of numbers
list1 = [-10, -21, -4, 45, -66, 93]
# using list comprehension
pos_nos = [num for num in list1 if num >= 0]
print("Positive numbers in the list: ", *pos_nos)
Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64
[END] |
Python program to print positive numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/ | # Python program to print positive Numbers in a List
# list of numbers
list1 = [-10, 21, 4, -45, -66, 93, -11]
# we can also print positive no's using lambda exp.
pos_nos = list(filter(lambda x: (x >= 0), list1))
print("Positive numbers in the list: ", *pos_nos) | Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64 | Python program to print positive numbers in a list
# Python program to print positive Numbers in a List
# list of numbers
list1 = [-10, 21, 4, -45, -66, 93, -11]
# we can also print positive no's using lambda exp.
pos_nos = list(filter(lambda x: (x >= 0), list1))
print("Positive numbers in the list: ", *pos_nos)
Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64
[END] |
Python program to print positive numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/ | l = [12, -7, 5, 64, -14]
print([a for j, a in enumerate(l) if a >= 0]) | Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64 | Python program to print positive numbers in a list
l = [12, -7, 5, 64, -14]
print([a for j, a in enumerate(l) if a >= 0])
Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64
[END] |
Python program to print positive numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/ | # Python program to print positive numbers in a List
# list of numbers
list1 = [11, -21, 0, 45, 66, -93]
res = []
list2 = list(map(str, list1))
for i in range(0, len(list2)):
if not list2[i].startswith("-") and list2[i] != "0":
res.append(str(list1[i]))
res = " ".join(res)
print(res) | Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64 | Python program to print positive numbers in a list
# Python program to print positive numbers in a List
# list of numbers
list1 = [11, -21, 0, 45, 66, -93]
res = []
list2 = list(map(str, list1))
for i in range(0, len(list2)):
if not list2[i].startswith("-") and list2[i] != "0":
res.append(str(list1[i]))
res = " ".join(res)
print(res)
Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64
[END] |
Python program to print positive numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/ | # Python program to print Positive Numbers in a List
import numpy as np
# list of numbers
list1 = np.array([-10, -21, -4, 45, -66, 93])
# using numpy Array
pos_nos = list1[list1 >= 0]
print("Positive numbers in the list: ", *pos_nos) | Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64 | Python program to print positive numbers in a list
# Python program to print Positive Numbers in a List
import numpy as np
# list of numbers
list1 = np.array([-10, -21, -4, 45, -66, 93])
# using numpy Array
pos_nos = list1[list1 >= 0]
print("Positive numbers in the list: ", *pos_nos)
Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64
[END] |
Python program to print positive numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/ | # Function to print even numbers in a list
def PrintEven(itr, list1):
if itr == len(list1): # Base Condition
return
if list1[itr] >= 0:
print(list1[itr], end=" ")
PrintEven(itr + 1, list1) # Recursive Function Call
return
list1 = [-5, 7, -19, 10, 9] # list of numbers
PrintEven(0, list1) # Function Call
# This code is contributed by vinay pinjala | Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64 | Python program to print positive numbers in a list
# Function to print even numbers in a list
def PrintEven(itr, list1):
if itr == len(list1): # Base Condition
return
if list1[itr] >= 0:
print(list1[itr], end=" ")
PrintEven(itr + 1, list1) # Recursive Function Call
return
list1 = [-5, 7, -19, 10, 9] # list of numbers
PrintEven(0, list1) # Function Call
# This code is contributed by vinay pinjala
Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64
[END] |
Python program to print positive numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-positive-numbers-in-a-list/ | # Python program to print positive Numbers in a List
# list of numbers
list1 = [-10, 21, 4, -45, -66, 93, -11]
import operator
pos_nos = []
for i in list1:
if operator.ge(i, 0):
pos_nos.append(i)
print("Positive numbers in the list: ", pos_nos) | Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64 | Python program to print positive numbers in a list
# Python program to print positive Numbers in a List
# list of numbers
list1 = [-10, 21, 4, -45, -66, 93, -11]
import operator
pos_nos = []
for i in list1:
if operator.ge(i, 0):
pos_nos.append(i)
print("Positive numbers in the list: ", pos_nos)
Input: list1 = [12, -7, 5, 64, -14]
Output: 12, 5, 64
[END] |
Python program to print negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/ | # Python program to print negative Numbers in a List??????# list of numberslist1 = [11, -21, 0, 45, 66, -93]??????# iterating each number in listfor num in list1:??????????????????????????????# checking condition??????????????" ") | Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14 | Python program to print negative numbers in a list
# Python program to print negative Numbers in a List??????# list of numberslist1 = [11, -21, 0, 45, 66, -93]??????# iterating each number in listfor num in list1:??????????????????????????????# checking condition??????????????" ")
Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14
[END] |
Python program to print negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/ | # Python program to print negative Numbers in a List
# list of numbers
list1 = [-10, 21, -4, -45, -66, 93]
num = 0
# using while loop
while num < len(list1):
# checking condition
if list1[num] < 0:
print(list1[num], end=" ")
# increment num
num += 1 | Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14 | Python program to print negative numbers in a list
# Python program to print negative Numbers in a List
# list of numbers
list1 = [-10, 21, -4, -45, -66, 93]
num = 0
# using while loop
while num < len(list1):
# checking condition
if list1[num] < 0:
print(list1[num], end=" ")
# increment num
num += 1
Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14
[END] |
Python program to print negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/ | # Python program to print negative Numbers in a List
# list of numbers
list1 = [-10, -21, -4, 45, -66, 93]
# using list comprehension
neg_nos = [num for num in list1 if num < 0]
print("Negative numbers in the list: ", *neg_nos) | Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14 | Python program to print negative numbers in a list
# Python program to print negative Numbers in a List
# list of numbers
list1 = [-10, -21, -4, 45, -66, 93]
# using list comprehension
neg_nos = [num for num in list1 if num < 0]
print("Negative numbers in the list: ", *neg_nos)
Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14
[END] |
Python program to print negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/ | # Python program to print negative Numbers in a List
# list of numbers
list1 = [-10, 21, 4, -45, -66, 93, -11]
# we can also print negative no's using lambda exp.
neg_nos = list(filter(lambda x: (x < 0), list1))
print("Negative numbers in the list: ", *neg_nos) | Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14 | Python program to print negative numbers in a list
# Python program to print negative Numbers in a List
# list of numbers
list1 = [-10, 21, 4, -45, -66, 93, -11]
# we can also print negative no's using lambda exp.
neg_nos = list(filter(lambda x: (x < 0), list1))
print("Negative numbers in the list: ", *neg_nos)
Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14
[END] |
Python program to print negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/ | l = [12, -7, 5, 64, -14]
print([a for j, a in enumerate(l) if a < 0]) | Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14 | Python program to print negative numbers in a list
l = [12, -7, 5, 64, -14]
print([a for j, a in enumerate(l) if a < 0])
Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14
[END] |
Python program to print negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/ | # Python program to print negative Numbers in a List
# list of numbers
list1 = [11, -21, 0, 45, 66, -93]
res = []
list2 = list(map(str, list1))
for i in range(0, len(list2)):
if list2[i].startswith("-"):
res.append(str(list1[i]))
res = " ".join(res)
print(res) | Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14 | Python program to print negative numbers in a list
# Python program to print negative Numbers in a List
# list of numbers
list1 = [11, -21, 0, 45, 66, -93]
res = []
list2 = list(map(str, list1))
for i in range(0, len(list2)):
if list2[i].startswith("-"):
res.append(str(list1[i]))
res = " ".join(res)
print(res)
Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14
[END] |
Python program to print negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/ | # Python program to print negative Numbers in a List
import numpy as np
# list of numbers
list1 = [-10, 21, 4, -45, -66, 93, -11]
# Using numpy to print the negative number
temp = np.array(list1)
neg_nos = temp[temp <= 0]
print("Negative numbers in the list: ", *neg_nos) | Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14 | Python program to print negative numbers in a list
# Python program to print negative Numbers in a List
import numpy as np
# list of numbers
list1 = [-10, 21, 4, -45, -66, 93, -11]
# Using numpy to print the negative number
temp = np.array(list1)
neg_nos = temp[temp <= 0]
print("Negative numbers in the list: ", *neg_nos)
Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14
[END] |
Python program to print negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/ | # Recursive function to check current element negative or not
def PrintNegative(itr, list1):
if itr == len(list1): # base condition
return
if list1[itr] < 0: # check negative number or not
print(list1[itr], end=" ")
PrintNegative(itr + 1, list1) # recursive function call
list1 = [-1, 8, 9, -5, 7]
PrintNegative(0, list1) | Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14 | Python program to print negative numbers in a list
# Recursive function to check current element negative or not
def PrintNegative(itr, list1):
if itr == len(list1): # base condition
return
if list1[itr] < 0: # check negative number or not
print(list1[itr], end=" ")
PrintNegative(itr + 1, list1) # recursive function call
list1 = [-1, 8, 9, -5, 7]
PrintNegative(0, list1)
Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14
[END] |
Python program to print negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/ | import numpy as np
# list of numbers
list1 = [12, -7, 5, 64, -14]
# converting list to numpy array
arr1 = np.array(list1)
# finding negative numbers in the array
neg_nums = arr1[np.where(arr1 < 0)]
# printing negative numbers
print("Negative numbers in the list: ", *neg_nums) | Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14 | Python program to print negative numbers in a list
import numpy as np
# list of numbers
list1 = [12, -7, 5, 64, -14]
# converting list to numpy array
arr1 = np.array(list1)
# finding negative numbers in the array
neg_nums = arr1[np.where(arr1 < 0)]
# printing negative numbers
print("Negative numbers in the list: ", *neg_nums)
Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14
[END] |
Python program to print negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/ | # Python program to print negative Numbers in a List
from functools import reduce
# list of numbers
list1 = [-10, -21, -4, 45, -66, 93]
# using reduce method
neg_nos = reduce(lambda a, b: a + [b] if b < 0 else a, list1, [])
print("Negative numbers in the list: ", *neg_nos) | Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14 | Python program to print negative numbers in a list
# Python program to print negative Numbers in a List
from functools import reduce
# list of numbers
list1 = [-10, -21, -4, 45, -66, 93]
# using reduce method
neg_nos = reduce(lambda a, b: a + [b] if b < 0 else a, list1, [])
print("Negative numbers in the list: ", *neg_nos)
Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14
[END] |
Python program to print all positive numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/ | # Python program to print positive Numbers in given range
start, end = -4, 19
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num >= 0:
print(num, end=" ") | Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
| Python program to print all positive numbers in a range
# Python program to print positive Numbers in given range
start, end = -4, 19
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num >= 0:
print(num, end=" ")
Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
[END] |
Python program to print all positive numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/ | # Python program to print positive Numbers in given range
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num >= 0:
print(num, end=" ") | Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
| Python program to print all positive numbers in a range
# Python program to print positive Numbers in given range
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num >= 0:
print(num, end=" ")
Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
[END] |
Python program to print all positive numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/ | # Python code To print all positive numbers
# in a given range using the lambda function
a = -4
b = 5
li = []
for i in range(a, b + 1):
li.append(i)
# printing positive numbers using the lambda function
positive_num = list(filter(lambda x: (x >= 0), li))
print(positive_num) | Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
| Python program to print all positive numbers in a range
# Python code To print all positive numbers
# in a given range using the lambda function
a = -4
b = 5
li = []
for i in range(a, b + 1):
li.append(i)
# printing positive numbers using the lambda function
positive_num = list(filter(lambda x: (x >= 0), li))
print(positive_num)
Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
[END] |
Python program to print all positive numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/ | # Python code
# To print all positive numbers in a given range
a = -4
b = 5
out = [i for i in range(a, b + 1) if i > 0]
# print the all positive numbers
print(*out) | Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
| Python program to print all positive numbers in a range
# Python code
# To print all positive numbers in a given range
a = -4
b = 5
out = [i for i in range(a, b + 1) if i > 0]
# print the all positive numbers
print(*out)
Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
[END] |
Python program to print all positive numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/ | a = -4
b = 5
l = []
for i in range(a, b + 1):
l.append(i)
print([a for j, a in enumerate(l) if a >= 0]) | Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
| Python program to print all positive numbers in a range
a = -4
b = 5
l = []
for i in range(a, b + 1):
l.append(i)
print([a for j, a in enumerate(l) if a >= 0])
Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
[END] |
Python program to print all positive numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/ | a = -4
b = 5
for i in range(a, b + 1):
if i < 0:
pass
else:
print(i, end=" ") | Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
| Python program to print all positive numbers in a range
a = -4
b = 5
for i in range(a, b + 1):
if i < 0:
pass
else:
print(i, end=" ")
Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
[END] |
Python program to print all positive numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/ | def printPositives(start, end): # defining recursive function to print positives
if start == end:
return # base condition
if start >= 0: # check for positive number
print(start, end=" ")
printPositives(start + 1, end) # recursive calling
a, b = -5, 10
printPositives(a, b) # function calling | Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
| Python program to print all positive numbers in a range
def printPositives(start, end): # defining recursive function to print positives
if start == end:
return # base condition
if start >= 0: # check for positive number
print(start, end=" ")
printPositives(start + 1, end) # recursive calling
a, b = -5, 10
printPositives(a, b) # function calling
Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
[END] |
Python program to print all positive numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-positive-numbers-in-a-range/ | a = -4
b = 5
positive_nums = list(filter(lambda x: x >= 0, range(a, b + 1)))
print(positive_nums) | Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
| Python program to print all positive numbers in a range
a = -4
b = 5
positive_nums = list(filter(lambda x: x >= 0, range(a, b + 1)))
print(positive_nums)
Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5
[END] |
Python program to print all negative numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/ | # Python code
# To print all negative numbers in a given range
def negativenumbers(a, b):
# Checking condition for negative numbers
# single line solution
out = [i for i in range(a, b + 1) if i < 0]
# print the all negative numbers
print(*out)
# driver code
# a -> start range
a = -4
# b -> end range
b = 5
negativenumbers(a, b) | Input: a = -4, b = 5
Output: -4, -3, -2, -1 | Python program to print all negative numbers in a range
# Python code
# To print all negative numbers in a given range
def negativenumbers(a, b):
# Checking condition for negative numbers
# single line solution
out = [i for i in range(a, b + 1) if i < 0]
# print the all negative numbers
print(*out)
# driver code
# a -> start range
a = -4
# b -> end range
b = 5
negativenumbers(a, b)
Input: a = -4, b = 5
Output: -4, -3, -2, -1
[END] |
Python program to print all negative numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/ | # Python program to print negative Numbers in given range
start, end = -4, 19
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num < 0:
print(num, end=" ") | Input: a = -4, b = 5
Output: -4, -3, -2, -1 | Python program to print all negative numbers in a range
# Python program to print negative Numbers in given range
start, end = -4, 19
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num < 0:
print(num, end=" ")
Input: a = -4, b = 5
Output: -4, -3, -2, -1
[END] |
Python program to print all negative numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/ | # Python program to print negative Numbers in given range
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num < 0:
print(num, end=" ") | Input: a = -4, b = 5
Output: -4, -3, -2, -1 | Python program to print all negative numbers in a range
# Python program to print negative Numbers in given range
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
# iterating each number in list
for num in range(start, end + 1):
# checking condition
if num < 0:
print(num, end=" ")
Input: a = -4, b = 5
Output: -4, -3, -2, -1
[END] |
Python program to print all negative numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/ | # Python code To print all negative
# numbers in a given range using lambda function
# inputs
a = -4
b = 5
li = []
for i in range(a, b):
li.append(i)
# printing negative numbers using the lambda function
negative_num = list(filter(lambda x: (x < 0), li))
print(negative_num) | Input: a = -4, b = 5
Output: -4, -3, -2, -1 | Python program to print all negative numbers in a range
# Python code To print all negative
# numbers in a given range using lambda function
# inputs
a = -4
b = 5
li = []
for i in range(a, b):
li.append(i)
# printing negative numbers using the lambda function
negative_num = list(filter(lambda x: (x < 0), li))
print(negative_num)
Input: a = -4, b = 5
Output: -4, -3, -2, -1
[END] |
Python program to print all negative numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/ | a = -4
b = 5
l = []
for i in range(a, b + 1):
l.append(i)
print([a for j, a in enumerate(l) if a < 0]) | Input: a = -4, b = 5
Output: -4, -3, -2, -1 | Python program to print all negative numbers in a range
a = -4
b = 5
l = []
for i in range(a, b + 1):
l.append(i)
print([a for j, a in enumerate(l) if a < 0])
Input: a = -4, b = 5
Output: -4, -3, -2, -1
[END] |
Python program to print all negative numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/ | a = -4
b = 5
print([i for i in range(a, b + 1) if i < 0]) | Input: a = -4, b = 5
Output: -4, -3, -2, -1 | Python program to print all negative numbers in a range
a = -4
b = 5
print([i for i in range(a, b + 1) if i < 0])
Input: a = -4, b = 5
Output: -4, -3, -2, -1
[END] |
Python program to print all negative numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/ | a = -4
b = 5
for i in range(a, b + 1):
if i >= 0:
pass
else:
print(i, end=" ") | Input: a = -4, b = 5
Output: -4, -3, -2, -1 | Python program to print all negative numbers in a range
a = -4
b = 5
for i in range(a, b + 1):
if i >= 0:
pass
else:
print(i, end=" ")
Input: a = -4, b = 5
Output: -4, -3, -2, -1
[END] |
Python program to print all negative numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/ | # Recursive function to print Negative numbers
def PrintNegative(itr, end):
if itr == end: # Base Condition
return
if itr < 0: # checking Negative or not
print(itr, end=" ")
PrintNegative(itr + 1, end) # Recursive function call
return
a = -5
b = 5
PrintNegative(a, b) | Input: a = -4, b = 5
Output: -4, -3, -2, -1 | Python program to print all negative numbers in a range
# Recursive function to print Negative numbers
def PrintNegative(itr, end):
if itr == end: # Base Condition
return
if itr < 0: # checking Negative or not
print(itr, end=" ")
PrintNegative(itr + 1, end) # Recursive function call
return
a = -5
b = 5
PrintNegative(a, b)
Input: a = -4, b = 5
Output: -4, -3, -2, -1
[END] |
Python program to print all negative numbers in a range | https://www.geeksforgeeks.org/python-program-to-print-all-negative-numbers-in-a-range/ | import numpy as np
# Taking input values for start and end of the range
start = -4
end = 5
# Creating an array using numpy.arange()
arr = np.arange(start, end + 1)
# Filtering negative numbers using numpy.where()
neg_arr = arr[np.where(arr < 0)]
# Printing the resulting array
print(neg_arr) | Input: a = -4, b = 5
Output: -4, -3, -2, -1 | Python program to print all negative numbers in a range
import numpy as np
# Taking input values for start and end of the range
start = -4
end = 5
# Creating an array using numpy.arange()
arr = np.arange(start, end + 1)
# Filtering negative numbers using numpy.where()
neg_arr = arr[np.where(arr < 0)]
# Printing the resulting array
print(neg_arr)
Input: a = -4, b = 5
Output: -4, -3, -2, -1
[END] |
Python program to count positive and negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/ | # Python program to count positive and negative numbers in a List
# list of numbers
list1 = [10, -21, 4, -45, 66, -93, 1]
pos_count, neg_count = 0, 0
# iterating each number in list
for num in list1:
# checking condition
if num >= 0:
pos_count += 1
else:
neg_count += 1
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list: ", neg_count) | Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3 | Python program to count positive and negative numbers in a list
# Python program to count positive and negative numbers in a List
# list of numbers
list1 = [10, -21, 4, -45, 66, -93, 1]
pos_count, neg_count = 0, 0
# iterating each number in list
for num in list1:
# checking condition
if num >= 0:
pos_count += 1
else:
neg_count += 1
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list: ", neg_count)
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3
[END] |
Python program to count positive and negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/ | # Python program to count positive and negative numbers in a List
# list of numbers
list1 = [-10, -21, -4, -45, -66, 93, 11]
pos_count, neg_count = 0, 0
num = 0
# using while loop
while num < len(list1):
# checking condition
if list1[num] >= 0:
pos_count += 1
else:
neg_count += 1
# increment num
num += 1
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list: ", neg_count) | Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3 | Python program to count positive and negative numbers in a list
# Python program to count positive and negative numbers in a List
# list of numbers
list1 = [-10, -21, -4, -45, -66, 93, 11]
pos_count, neg_count = 0, 0
num = 0
# using while loop
while num < len(list1):
# checking condition
if list1[num] >= 0:
pos_count += 1
else:
neg_count += 1
# increment num
num += 1
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list: ", neg_count)
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3
[END] |
Python program to count positive and negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/ | # Python program to count positive
# and negative numbers in a List
# list of numbers
list1 = [10, -21, -4, 45, 66, 93, -11]
neg_count = len(list(filter(lambda x: (x < 0), list1)))
# we can also do len(list1) - neg_count
pos_count = len(list(filter(lambda x: (x >= 0), list1)))
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list: ", neg_count) | Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3 | Python program to count positive and negative numbers in a list
# Python program to count positive
# and negative numbers in a List
# list of numbers
list1 = [10, -21, -4, 45, 66, 93, -11]
neg_count = len(list(filter(lambda x: (x < 0), list1)))
# we can also do len(list1) - neg_count
pos_count = len(list(filter(lambda x: (x >= 0), list1)))
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list: ", neg_count)
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3
[END] |
Python program to count positive and negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/ | # Python program to count positive
# and negative numbers in a List
# list of numbers
list1 = [-10, -21, -4, -45, -66, -93, 11]
only_pos = [num for num in list1 if num >= 1]
pos_count = len(only_pos)
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list: ", len(list1) - pos_count) | Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3 | Python program to count positive and negative numbers in a list
# Python program to count positive
# and negative numbers in a List
# list of numbers
list1 = [-10, -21, -4, -45, -66, -93, 11]
only_pos = [num for num in list1 if num >= 1]
pos_count = len(only_pos)
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list: ", len(list1) - pos_count)
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3
[END] |
Python program to count positive and negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/ | l = [12, -7, 5, 64, -14]
c = 0
x = [a for j, a in enumerate(l) if a >= 0]
print("Length of Positive numbers is:", len(x))
print("Length of Negative numbers is:", len(l) - len(x)) | Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3 | Python program to count positive and negative numbers in a list
l = [12, -7, 5, 64, -14]
c = 0
x = [a for j, a in enumerate(l) if a >= 0]
print("Length of Positive numbers is:", len(x))
print("Length of Negative numbers is:", len(l) - len(x))
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3
[END] |
Python program to count positive and negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/ | # Python program to count positive and negative numbers in a List
# list of numbers
list1 = [10, -21, 4, -45, 66, -93, 1, 0]
pos_count, neg_count = 0, 0
list2 = list(map(str, list1))
# iterating each number in list
for num in list2:
# checking condition
if num.startswith("-"):
neg_count += 1
elif num != "0":
if not num.startswith("-"):
pos_count += 1
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list: ", neg_count) | Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3 | Python program to count positive and negative numbers in a list
# Python program to count positive and negative numbers in a List
# list of numbers
list1 = [10, -21, 4, -45, 66, -93, 1, 0]
pos_count, neg_count = 0, 0
list2 = list(map(str, list1))
# iterating each number in list
for num in list2:
# checking condition
if num.startswith("-"):
neg_count += 1
elif num != "0":
if not num.startswith("-"):
pos_count += 1
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list: ", neg_count)
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3
[END] |
Python program to count positive and negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/ | l = [12, -7, 5, 64, -14]
x = sum(1 for i in l if i >= 0)
print("Length of Positive numbers is:", x)
print("Length of Negative numbers is:", len(l) - x) | Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3 | Python program to count positive and negative numbers in a list
l = [12, -7, 5, 64, -14]
x = sum(1 for i in l if i >= 0)
print("Length of Positive numbers is:", x)
print("Length of Negative numbers is:", len(l) - x)
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3
[END] |
Python program to count positive and negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/ | # Python program to count positive and negative numbers in a List
# list of numbers
list1 = [10, -21, 4, -45, 66, -93, 1, 0]
pos_count, neg_count = 0, 0
list2 = list(map(str, list1))
# iterating each number in list
for num in list2:
# checking condition
if num[0] == "-":
neg_count += 1
elif num != "0":
if not num[0] == "-":
pos_count += 1
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list: ", neg_count) | Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3 | Python program to count positive and negative numbers in a list
# Python program to count positive and negative numbers in a List
# list of numbers
list1 = [10, -21, 4, -45, 66, -93, 1, 0]
pos_count, neg_count = 0, 0
list2 = list(map(str, list1))
# iterating each number in list
for num in list2:
# checking condition
if num[0] == "-":
neg_count += 1
elif num != "0":
if not num[0] == "-":
pos_count += 1
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list: ", neg_count)
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3
[END] |
Python program to count positive and negative numbers in a list | https://www.geeksforgeeks.org/python-program-to-count-positive-and-negative-numbers-in-a-list/ | # Python program to count positive and negative numbers in a List
from collections import Counter
# list of numbers
list1 = [10, -21, 4, -45, 66, -93, 1, 0]
# use the Counter module to count the number of positive and negative numbers in the list
counts = Counter(num > 0 for num in list1)
# get the count of positive and negative numbers excluding 0 from positive as well as negative
pos_count = counts[True]
neg_count = counts[False] - (0 in list1)
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list: ", neg_count) | Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3 | Python program to count positive and negative numbers in a list
# Python program to count positive and negative numbers in a List
from collections import Counter
# list of numbers
list1 = [10, -21, 4, -45, 66, -93, 1, 0]
# use the Counter module to count the number of positive and negative numbers in the list
counts = Counter(num > 0 for num in list1)
# get the count of positive and negative numbers excluding 0 from positive as well as negative
pos_count = counts[True]
neg_count = counts[False] - (0 in list1)
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list: ", neg_count)
Input: list1 = [2, -7, 5, -64, -14]
Output: pos = 2, neg = 3
[END] |
Remove multiple elements from a list in Python | https://www.geeksforgeeks.org/remove-multiple-elements-from-a-list-in-python/ | # Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# Iterate each element in list
# and add them in variable total
for ele in list1:
if ele % 2 == 0:
list1.remove(ele)
# printing modified list
print("New list after removing all even numbers: ", list1) | Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10] | Remove multiple elements from a list in Python
# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# Iterate each element in list
# and add them in variable total
for ele in list1:
if ele % 2 == 0:
list1.remove(ele)
# printing modified list
print("New list after removing all even numbers: ", list1)
Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]
[END] |
Remove multiple elements from a list in Python | https://www.geeksforgeeks.org/remove-multiple-elements-from-a-list-in-python/ | # Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# will create a new list,
# excluding all even numbers
list1 = [elem for elem in list1 if elem % 2 != 0]
print(*list1) | Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10] | Remove multiple elements from a list in Python
# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# will create a new list,
# excluding all even numbers
list1 = [elem for elem in list1 if elem % 2 != 0]
print(*list1)
Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]
[END] |
Remove multiple elements from a list in Python | https://www.geeksforgeeks.org/remove-multiple-elements-from-a-list-in-python/ | # Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# removes elements from index 1 to 4
# i.e. 5, 17, 18, 23 will be deleted
del list1[1:5]
print(*list1) | Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10] | Remove multiple elements from a list in Python
# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# removes elements from index 1 to 4
# i.e. 5, 17, 18, 23 will be deleted
del list1[1:5]
print(*list1)
Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]
[END] |
Remove multiple elements from a list in Python | https://www.geeksforgeeks.org/remove-multiple-elements-from-a-list-in-python/ | # Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# items to be removed
unwanted_num = {11, 5}
list1 = [ele for ele in list1 if ele not in unwanted_num]
# printing modified list
print("New list after removing unwanted numbers: ", list1) | Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10] | Remove multiple elements from a list in Python
# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# items to be removed
unwanted_num = {11, 5}
list1 = [ele for ele in list1 if ele not in unwanted_num]
# printing modified list
print("New list after removing unwanted numbers: ", list1)
Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]
[END] |
Remove multiple elements from a list in Python | https://www.geeksforgeeks.org/remove-multiple-elements-from-a-list-in-python/ | # Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# given index of elements
# removes 11, 18, 23
unwanted = [0, 3, 4]
for ele in sorted(unwanted, reverse=True):
del list1[ele]
# printing modified list
print(*list1) | Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10] | Remove multiple elements from a list in Python
# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# given index of elements
# removes 11, 18, 23
unwanted = [0, 3, 4]
for ele in sorted(unwanted, reverse=True):
del list1[ele]
# printing modified list
print(*list1)
Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]
[END] |
Remove multiple elements from a list in Python | https://www.geeksforgeeks.org/remove-multiple-elements-from-a-list-in-python/ | list1 = [11, 5, 17, 18, 23, 50]
list1 = [elem for i, elem in enumerate(list1) if elem % 2 != 0]
print(list1) | Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10] | Remove multiple elements from a list in Python
list1 = [11, 5, 17, 18, 23, 50]
list1 = [elem for i, elem in enumerate(list1) if elem % 2 != 0]
print(list1)
Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]
[END] |
Remove multiple elements from a list in Python | https://www.geeksforgeeks.org/remove-multiple-elements-from-a-list-in-python/ | import numpy as np
# creating a list
list1 = [12, 15, 3, 10]
# convert list to numpy array
arr = np.array(list1)
# indices of elements to be removed
remove_idx = [0, 2]
# use numpy.delete() to remove elements
new_arr = np.delete(arr, remove_idx)
# convert numpy array back to list
new_list = new_arr.tolist()
# print the results
print("Removed =", [list1[i] for i in remove_idx], ", New_List =", new_list) | Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10] | Remove multiple elements from a list in Python
import numpy as np
# creating a list
list1 = [12, 15, 3, 10]
# convert list to numpy array
arr = np.array(list1)
# indices of elements to be removed
remove_idx = [0, 2]
# use numpy.delete() to remove elements
new_arr = np.delete(arr, remove_idx)
# convert numpy array back to list
new_list = new_arr.tolist()
# print the results
print("Removed =", [list1[i] for i in remove_idx], ", New_List =", new_list)
Input: [12, 15, 3, 10]
Output: Remove = [12, 3], New_List = [15, 10]
[END] |
Python | Remove empty tuples from a list | https://www.geeksforgeeks.org/python-remove-empty-tuples-list/ | # Python program to remove empty tuples from a
# list of tuples function to remove empty tuples
# using list comprehension
def Remove(tuples):
tuples = [t for t in tuples if t]
return tuples
# Driver Code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
print(Remove(tuples)) |
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] | Python | Remove empty tuples from a list
# Python program to remove empty tuples from a
# list of tuples function to remove empty tuples
# using list comprehension
def Remove(tuples):
tuples = [t for t in tuples if t]
return tuples
# Driver Code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
print(Remove(tuples))
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]
[END] |
Python | Remove empty tuples from a list | https://www.geeksforgeeks.org/python-remove-empty-tuples-list/ | # Python2 program to remove empty tuples# from a list of tuples function to remove# empty tuples using filterdef Remove(tuples):????????????????????????tuples = filter(None, tuples)????????????????????????return tuples??????# Driver Codetuples = [(), ('ram','15','8'), (), ('laxman', 'sita'),??????????????????????????????????????????? |
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] | Python | Remove empty tuples from a list
# Python2 program to remove empty tuples# from a list of tuples function to remove# empty tuples using filterdef Remove(tuples):????????????????????????tuples = filter(None, tuples)????????????????????????return tuples??????# Driver Codetuples = [(), ('ram','15','8'), (), ('laxman', 'sita'),???????????????????????????????????????????
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]
[END] |
Python | Remove empty tuples from a list | https://www.geeksforgeeks.org/python-remove-empty-tuples-list/ | # Python program to remove empty tuples from
# a list of tuples function to remove empty
# tuples using filter
def Remove(tuples):
tuples = filter(None, tuples)
return tuples
# Driver Code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
print(Remove(tuples)) |
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] | Python | Remove empty tuples from a list
# Python program to remove empty tuples from
# a list of tuples function to remove empty
# tuples using filter
def Remove(tuples):
tuples = filter(None, tuples)
return tuples
# Driver Code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
print(Remove(tuples))
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]
[END] |
Python | Remove empty tuples from a list | https://www.geeksforgeeks.org/python-remove-empty-tuples-list/ | # Python program to remove empty tuples from a
# list of tuples function to remove empty tuples
# using len()
def Remove(tuples):
for i in tuples:
if len(i) == 0:
tuples.remove(i)
return tuples
# Driver Code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
print(Remove(tuples)) |
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] | Python | Remove empty tuples from a list
# Python program to remove empty tuples from a
# list of tuples function to remove empty tuples
# using len()
def Remove(tuples):
for i in tuples:
if len(i) == 0:
tuples.remove(i)
return tuples
# Driver Code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
print(Remove(tuples))
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]
[END] |
Python | Remove empty tuples from a list | https://www.geeksforgeeks.org/python-remove-empty-tuples-list/ | # Python program to remove empty tuples from a
# list of tuples function to remove empty tuples
def Remove(tuples):
for i in tuples:
if i == ():
tuples.remove(i)
return tuples
# Driver Code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
print(Remove(tuples)) |
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] | Python | Remove empty tuples from a list
# Python program to remove empty tuples from a
# list of tuples function to remove empty tuples
def Remove(tuples):
for i in tuples:
if i == ():
tuples.remove(i)
return tuples
# Driver Code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
print(Remove(tuples))
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]
[END] |
Python | Remove empty tuples from a list | https://www.geeksforgeeks.org/python-remove-empty-tuples-list/ | tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
res = [t for i, t in enumerate(tuples) if t]
print(res) |
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] | Python | Remove empty tuples from a list
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
res = [t for i, t in enumerate(tuples) if t]
print(res)
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]
[END] |
Python | Remove empty tuples from a list | https://www.geeksforgeeks.org/python-remove-empty-tuples-list/ | # Python program to remove empty tuples from
# a list of tuples function to remove empty
# tuples using while loop and in operator
def Remove(tuples):
while () in tuples:
tuples.remove(())
return tuples
# Driver Code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
print(Remove(tuples)) |
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] | Python | Remove empty tuples from a list
# Python program to remove empty tuples from
# a list of tuples function to remove empty
# tuples using while loop and in operator
def Remove(tuples):
while () in tuples:
tuples.remove(())
return tuples
# Driver Code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
print(Remove(tuples))
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]
[END] |
Python | Remove empty tuples from a list | https://www.geeksforgeeks.org/python-remove-empty-tuples-list/ | # Python program to remove empty tuples from a
# list of tuples function to remove empty tuples
def Remove(tuples):
for i in tuples:
if str(i).find("()") != -1 and len(str(i)) == 2:
tuples.remove(i)
return tuples
# Driver Code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
print(Remove(tuples)) |
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] | Python | Remove empty tuples from a list
# Python program to remove empty tuples from a
# list of tuples function to remove empty tuples
def Remove(tuples):
for i in tuples:
if str(i).find("()") != -1 and len(str(i)) == 2:
tuples.remove(i)
return tuples
# Driver Code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
print(Remove(tuples))
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]
[END] |
Python | Remove empty tuples from a list | https://www.geeksforgeeks.org/python-remove-empty-tuples-list/ | # Python program to remove empty tuples from
# a list of tuples function to remove empty
# tuples using lambda function
# Driver Code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
tuples = list(filter(lambda x: len(x) > 0, tuples))
print(tuples) |
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] | Python | Remove empty tuples from a list
# Python program to remove empty tuples from
# a list of tuples function to remove empty
# tuples using lambda function
# Driver Code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
tuples = list(filter(lambda x: len(x) > 0, tuples))
print(tuples)
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]
[END] |
Python | Remove empty tuples from a list | https://www.geeksforgeeks.org/python-remove-empty-tuples-list/ | def remove_empty_tuples(tuples):
return [t for t in tuples if len(t) > 0]
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
print(remove_empty_tuples(tuples))
# This code is contributed by Edula Vinay Kumar Reddy |
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] | Python | Remove empty tuples from a list
def remove_empty_tuples(tuples):
return [t for t in tuples if len(t) > 0]
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
print(remove_empty_tuples(tuples))
# This code is contributed by Edula Vinay Kumar Reddy
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]
[END] |
Python | Remove empty tuples from a list | https://www.geeksforgeeks.org/python-remove-empty-tuples-list/ | # defining recursive function to remove empty tuples in a list
def remove_empty_tuples(start, oldlist, newlist):
if start == len(oldlist): # base condition
return newlist
if oldlist[start] == (): # checking the element is empty tuple or not
pass
else:
newlist.append(oldlist[start]) # appending non empty tuple element to newlist
return remove_empty_tuples(start + 1, oldlist, newlist) # recursive function call
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
# print('The original list: ',tuples)
print(remove_empty_tuples(0, tuples, []))
# This code is contributed by tvsk |
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] | Python | Remove empty tuples from a list
# defining recursive function to remove empty tuples in a list
def remove_empty_tuples(start, oldlist, newlist):
if start == len(oldlist): # base condition
return newlist
if oldlist[start] == (): # checking the element is empty tuple or not
pass
else:
newlist.append(oldlist[start]) # appending non empty tuple element to newlist
return remove_empty_tuples(start + 1, oldlist, newlist) # recursive function call
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
# print('The original list: ',tuples)
print(remove_empty_tuples(0, tuples, []))
# This code is contributed by tvsk
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]
[END] |
Python | Remove empty tuples from a list | https://www.geeksforgeeks.org/python-remove-empty-tuples-list/ | import itertools # import the itertools module
def Remove(tuples):
tuples = list(
itertools.filterfalse(lambda x: x == (), tuples)
) # remove empty tuples using filterfalse from itertools
return tuples
# Driver code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
] # define the input list of tuples
print(Remove(tuples)) # call the Remove function and print the output |
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] | Python | Remove empty tuples from a list
import itertools # import the itertools module
def Remove(tuples):
tuples = list(
itertools.filterfalse(lambda x: x == (), tuples)
) # remove empty tuples using filterfalse from itertools
return tuples
# Driver code
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
] # define the input list of tuples
print(Remove(tuples)) # call the Remove function and print the output
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]
[END] |
Python | Remove empty tuples from a list | https://www.geeksforgeeks.org/python-remove-empty-tuples-list/ | from functools import reduce
# defining lambda function to remove empty tuples
remove_empty = lambda lst, val: lst + [val] if val != () else lst
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
# using reduce to remove empty tuples
result = reduce(remove_empty, tuples, [])
# printing final result
print("The original list is : " + str(tuples))
print("The list after removing empty tuples : " + str(result))
# This code is contributed by Rayudu. |
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')] | Python | Remove empty tuples from a list
from functools import reduce
# defining lambda function to remove empty tuples
remove_empty = lambda lst, val: lst + [val] if val != () else lst
tuples = [
(),
("ram", "15", "8"),
(),
("laxman", "sita"),
("krishna", "akbar", "45"),
("", ""),
(),
]
# using reduce to remove empty tuples
result = reduce(remove_empty, tuples, [])
# printing final result
print("The original list is : " + str(tuples))
print("The list after removing empty tuples : " + str(result))
# This code is contributed by Rayudu.
#Output : [('ram', '15', '8'), ('laxman', 'sita'), ('krishna', 'akbar', '45'), ('', '')]
[END] |
Python | Program to print duplicates from a list of integers | https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/ | # Python program to print
# duplicates from a list
# of integers
def Repeat(x):
_size = len(x)
repeated = []
for i in range(_size):
k = i + 1
for j in range(k, _size):
if x[i] == x[j] and x[i] not in repeated:
repeated.append(x[i])
return repeated
# Driver Code
list1 = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
print(Repeat(list1))
# This code is contributed
# by Sandeep_anand | #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60] | Python | Program to print duplicates from a list of integers
# Python program to print
# duplicates from a list
# of integers
def Repeat(x):
_size = len(x)
repeated = []
for i in range(_size):
k = i + 1
for j in range(k, _size):
if x[i] == x[j] and x[i] not in repeated:
repeated.append(x[i])
return repeated
# Driver Code
list1 = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
print(Repeat(list1))
# This code is contributed
# by Sandeep_anand
#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60]
[END] |
Python | Program to print duplicates from a list of integers | https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/ | # Python program to print duplicates from
# a list of integers
lis = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
uniqueList = []
duplicateList = []
for i in lis:
if i not in uniqueList:
uniqueList.append(i)
elif i not in duplicateList:
duplicateList.append(i)
print(duplicateList) | #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60] | Python | Program to print duplicates from a list of integers
# Python program to print duplicates from
# a list of integers
lis = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
uniqueList = []
duplicateList = []
for i in lis:
if i not in uniqueList:
uniqueList.append(i)
elif i not in duplicateList:
duplicateList.append(i)
print(duplicateList)
#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60]
[END] |
Python | Program to print duplicates from a list of integers | https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/ | from collections import Counter
l1 = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
d = Counter(l1)
print(d)
new_list = list([item for item in d if d[item] > 1])
print(new_list) | #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60] | Python | Program to print duplicates from a list of integers
from collections import Counter
l1 = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
d = Counter(l1)
print(d)
new_list = list([item for item in d if d[item] > 1])
print(new_list)
#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60]
[END] |
Python | Program to print duplicates from a list of integers | https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/ | # program to print duplicate numbers in a given list
# provided input
list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
new = [] # defining output list
# condition for reviewing every
# element of given input list
for a in list:
# checking the occurrence of elements
n = list.count(a)
# if the occurrence is more than
# one we add it to the output list
if n > 1:
if new.count(a) == 0: # condition to check
new.append(a)
print(new)
# This code is contributed by Himanshu Khune | #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60] | Python | Program to print duplicates from a list of integers
# program to print duplicate numbers in a given list
# provided input
list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
new = [] # defining output list
# condition for reviewing every
# element of given input list
for a in list:
# checking the occurrence of elements
n = list.count(a)
# if the occurrence is more than
# one we add it to the output list
if n > 1:
if new.count(a) == 0: # condition to check
new.append(a)
print(new)
# This code is contributed by Himanshu Khune
#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60]
[END] |
Python | Program to print duplicates from a list of integers | https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/ | def duplicate(input_list):
return list(set([x for x in input_list if input_list.count(x) > 1]))
if __name__ == "__main__":
input_list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
print(duplicate(input_list))
# This code is contributed by saikot | #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60] | Python | Program to print duplicates from a list of integers
def duplicate(input_list):
return list(set([x for x in input_list if input_list.count(x) > 1]))
if __name__ == "__main__":
input_list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
print(duplicate(input_list))
# This code is contributed by saikot
#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60]
[END] |
Python | Program to print duplicates from a list of integers | https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/ | def duplicate(input_list):
new_dict, new_list = {}, []
for i in input_list:
if not i in new_dict:
new_dict[i] = 1
else:
new_dict[i] += 1
for key, values in new_dict.items():
if values > 1:
new_list.append(key)
return new_list
if __name__ == "__main__":
input_list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
print(duplicate(input_list))
# This code is contributed by saikot | #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60] | Python | Program to print duplicates from a list of integers
def duplicate(input_list):
new_dict, new_list = {}, []
for i in input_list:
if not i in new_dict:
new_dict[i] = 1
else:
new_dict[i] += 1
for key, values in new_dict.items():
if values > 1:
new_list.append(key)
return new_list
if __name__ == "__main__":
input_list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
print(duplicate(input_list))
# This code is contributed by saikot
#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60]
[END] |
Python | Program to print duplicates from a list of integers | https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/ | lis = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
x = []
y = []
for i in lis:
if i not in x:
x.append(i)
for i in x:
if lis.count(i) > 1:
y.append(i)
print(y) | #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60] | Python | Program to print duplicates from a list of integers
lis = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
x = []
y = []
for i in lis:
if i not in x:
x.append(i)
for i in x:
if lis.count(i) > 1:
y.append(i)
print(y)
#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60]
[END] |
Python | Program to print duplicates from a list of integers | https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/ | input_list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
print(list(set([x for i, x in enumerate(input_list) if input_list.count(x) > 1]))) | #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60] | Python | Program to print duplicates from a list of integers
input_list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
print(list(set([x for i, x in enumerate(input_list) if input_list.count(x) > 1])))
#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60]
[END] |
Python | Program to print duplicates from a list of integers | https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/ | import operator as op
# program to print duplicate numbers in a given list
# provided input
list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
new = [] # defining output list
# condition for reviewing every
# element of given input list
for a in list:
# checking the occurrence of elements
n = op.countOf(list, a)
# if the occurrence is more than
# one we add it to the output list
if n > 1:
if op.countOf(new, a) == 0: # condition to check
new.append(a)
print(new)
# This code is contributed by vikkycirus | #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60] | Python | Program to print duplicates from a list of integers
import operator as op
# program to print duplicate numbers in a given list
# provided input
list = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
new = [] # defining output list
# condition for reviewing every
# element of given input list
for a in list:
# checking the occurrence of elements
n = op.countOf(list, a)
# if the occurrence is more than
# one we add it to the output list
if n > 1:
if op.countOf(new, a) == 0: # condition to check
new.append(a)
print(new)
# This code is contributed by vikkycirus
#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60]
[END] |
Python | Program to print duplicates from a list of integers | https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/ | lis = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
x = []
y = []
import operator
for i in lis:
if i not in x:
x.append(i)
for i in x:
if operator.countOf(lis, i) > 1:
y.append(i)
print(y) | #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60] | Python | Program to print duplicates from a list of integers
lis = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
x = []
y = []
import operator
for i in lis:
if i not in x:
x.append(i)
for i in x:
if operator.countOf(lis, i) > 1:
y.append(i)
print(y)
#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60]
[END] |
Python | Program to print duplicates from a list of integers | https://www.geeksforgeeks.org/python-program-print-duplicates-list-integers/ | import numpy as np
l1 = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
unique, counts = np.unique(l1, return_counts=True)
new_list = unique[np.where(counts > 1)]
print(new_list)
# This code is contributed by Rayudu | #Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60] | Python | Program to print duplicates from a list of integers
import numpy as np
l1 = [1, 2, 1, 2, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 9]
unique, counts = np.unique(l1, return_counts=True)
new_list = unique[np.where(counts > 1)]
print(new_list)
# This code is contributed by Rayudu
#Input : list = [10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]
#Output : output_list = [20, 30, -20, 60]
[END] |
Remove empty List from List | https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar | # Python3 code to Demonstrate Remove empty List
# from List using list comprehension
# Initializing list
test_list = [5, 6, [], 3, [], [], 9]
# printing original list
print("The original list is : " + str(test_list))
# Remove empty List from List
# using list comprehension
res = [ele for ele in test_list if ele != []]
# printing result
print("List after empty list removal : " + str(res)) |
#Output : The original list is : [5, 6, [], 3, [], [], 9] | Remove empty List from List
# Python3 code to Demonstrate Remove empty List
# from List using list comprehension
# Initializing list
test_list = [5, 6, [], 3, [], [], 9]
# printing original list
print("The original list is : " + str(test_list))
# Remove empty List from List
# using list comprehension
res = [ele for ele in test_list if ele != []]
# printing result
print("List after empty list removal : " + str(res))
#Output : The original list is : [5, 6, [], 3, [], [], 9]
[END] |
Remove empty List from List | https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar | # Python3 Code to Demonstrate Remove empty List
# from List using filter() Method
# Initializing list by custom values
test_list = [5, 6, [], 3, [], [], 9]
# Printing original list
print("The original list is : " + str(test_list))
# Removing empty List from List
# using filter() method
res = list(filter(None, test_list))
# Printing the resultant list
print("List after empty list removal : " + str(res)) |
#Output : The original list is : [5, 6, [], 3, [], [], 9] | Remove empty List from List
# Python3 Code to Demonstrate Remove empty List
# from List using filter() Method
# Initializing list by custom values
test_list = [5, 6, [], 3, [], [], 9]
# Printing original list
print("The original list is : " + str(test_list))
# Removing empty List from List
# using filter() method
res = list(filter(None, test_list))
# Printing the resultant list
print("List after empty list removal : " + str(res))
#Output : The original list is : [5, 6, [], 3, [], [], 9]
[END] |
Remove empty List from List | https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar | # Python Code to Remove empty List from List
def empty_list_remove(input_list):
new_list = []
for ele in input_list:
if ele:
new_list.append(ele)
return new_list
# input list values
input_list = [5, 6, [], 3, [], [], 9]
# print initial list values
print(f"The original list is : {input_list}")
# function-call & print values
print(f"List after empty list removal : {empty_list_remove(input_list)}") |
#Output : The original list is : [5, 6, [], 3, [], [], 9] | Remove empty List from List
# Python Code to Remove empty List from List
def empty_list_remove(input_list):
new_list = []
for ele in input_list:
if ele:
new_list.append(ele)
return new_list
# input list values
input_list = [5, 6, [], 3, [], [], 9]
# print initial list values
print(f"The original list is : {input_list}")
# function-call & print values
print(f"List after empty list removal : {empty_list_remove(input_list)}")
#Output : The original list is : [5, 6, [], 3, [], [], 9]
[END] |
Remove empty List from List | https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar | # Python3 code to Demonstrate Remove empty List
# Initializing list
test_list = [5, 6, [], 3, [], [], 9]
# printing original list
print("The original list is : " + str(test_list))
new_list = []
# Remove empty List from List
for i in test_list:
x = str(type(i))
if x.find("list") != -1:
if len(i) != 0:
new_list.append(i)
else:
new_list.append(i)
# printing result
print("List after empty list removal : " + str(new_list)) |
#Output : The original list is : [5, 6, [], 3, [], [], 9] | Remove empty List from List
# Python3 code to Demonstrate Remove empty List
# Initializing list
test_list = [5, 6, [], 3, [], [], 9]
# printing original list
print("The original list is : " + str(test_list))
new_list = []
# Remove empty List from List
for i in test_list:
x = str(type(i))
if x.find("list") != -1:
if len(i) != 0:
new_list.append(i)
else:
new_list.append(i)
# printing result
print("List after empty list removal : " + str(new_list))
#Output : The original list is : [5, 6, [], 3, [], [], 9]
[END] |
Remove empty List from List | https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar | # Python3 code to Demonstrate Remove empty List
# Initializing list
test_list = [5, 6, [], 3, [], [], 9]
# printing original list
print("The original list is : " + str(test_list))
# Remove empty List from List
while [] in test_list:
test_list.remove([])
# printing result
print("List after empty list removal : " + str(test_list)) |
#Output : The original list is : [5, 6, [], 3, [], [], 9] | Remove empty List from List
# Python3 code to Demonstrate Remove empty List
# Initializing list
test_list = [5, 6, [], 3, [], [], 9]
# printing original list
print("The original list is : " + str(test_list))
# Remove empty List from List
while [] in test_list:
test_list.remove([])
# printing result
print("List after empty list removal : " + str(test_list))
#Output : The original list is : [5, 6, [], 3, [], [], 9]
[END] |
Remove empty List from List | https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar | # Python3 code to Demonstrate Remove empty List
# Initializing list
test_list = [5, 6, [], 3, [], [], 9]
# printing original list
print("The original list is : " + str(test_list))
x = list(map(str, test_list))
y = "".join(x)
y = y.replace("[]", "")
y = list(map(int, y))
# printing result
print("List after empty list removal : " + str(y)) |
#Output : The original list is : [5, 6, [], 3, [], [], 9] | Remove empty List from List
# Python3 code to Demonstrate Remove empty List
# Initializing list
test_list = [5, 6, [], 3, [], [], 9]
# printing original list
print("The original list is : " + str(test_list))
x = list(map(str, test_list))
y = "".join(x)
y = y.replace("[]", "")
y = list(map(int, y))
# printing result
print("List after empty list removal : " + str(y))
#Output : The original list is : [5, 6, [], 3, [], [], 9]
[END] |
Remove empty List from List | https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar | test_list = [5, 6, [], 3, [], [], 9]
res = [ele for i, ele in enumerate(test_list) if ele != []]
print(res) |
#Output : The original list is : [5, 6, [], 3, [], [], 9] | Remove empty List from List
test_list = [5, 6, [], 3, [], [], 9]
res = [ele for i, ele in enumerate(test_list) if ele != []]
print(res)
#Output : The original list is : [5, 6, [], 3, [], [], 9]
[END] |
Remove empty List from List | https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar | # Python3 code to Demonstrate Remove empty List
# Initializing list
test_list = [5, 6, [], 3, [], [], 9]
# printing original list
print("The original list is : " + str(test_list))
# Remove empty List from List
res = filter(None, test_list)
# printing result
print("List after empty list removal : ", res) |
#Output : The original list is : [5, 6, [], 3, [], [], 9] | Remove empty List from List
# Python3 code to Demonstrate Remove empty List
# Initializing list
test_list = [5, 6, [], 3, [], [], 9]
# printing original list
print("The original list is : " + str(test_list))
# Remove empty List from List
res = filter(None, test_list)
# printing result
print("List after empty list removal : ", res)
#Output : The original list is : [5, 6, [], 3, [], [], 9]
[END] |
Remove empty List from List | https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar | # Python3 Code to Demonstrate Remove empty List
# from List using lambda function
# Initializing list by custom values
test_list = [5, 6, [], 3, [], [], 9]
# Printing original list
print("The original list is : " + str(test_list))
# Removing empty List from List
# using lambda function
res = list(filter(lambda x: x != [], test_list))
# Printing the resultant list
print("List after empty list removal : " + str(res)) |
#Output : The original list is : [5, 6, [], 3, [], [], 9] | Remove empty List from List
# Python3 Code to Demonstrate Remove empty List
# from List using lambda function
# Initializing list by custom values
test_list = [5, 6, [], 3, [], [], 9]
# Printing original list
print("The original list is : " + str(test_list))
# Removing empty List from List
# using lambda function
res = list(filter(lambda x: x != [], test_list))
# Printing the resultant list
print("List after empty list removal : " + str(res))
#Output : The original list is : [5, 6, [], 3, [], [], 9]
[END] |
Remove empty List from List | https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar | # Python3 code to Demonstrate Remove empty List
# defining recursive function to remove empty list
def remove_empty(start, oldlist, newlist):
if start == len(oldlist): # base condition
return newlist
if oldlist[start] == []: # checking the element is empty list or not
pass
else:
newlist.append(oldlist[start]) # appending non empty list element to newlist
return remove_empty(start + 1, oldlist, newlist) # recursive function call
test_list = [5, 6, [], 3, [], [], 9]
# printing original list
print("The original list is : " + str(test_list))
result = remove_empty(0, test_list, [])
# printing result
print("List after empty list removal : ", result) |
#Output : The original list is : [5, 6, [], 3, [], [], 9] | Remove empty List from List
# Python3 code to Demonstrate Remove empty List
# defining recursive function to remove empty list
def remove_empty(start, oldlist, newlist):
if start == len(oldlist): # base condition
return newlist
if oldlist[start] == []: # checking the element is empty list or not
pass
else:
newlist.append(oldlist[start]) # appending non empty list element to newlist
return remove_empty(start + 1, oldlist, newlist) # recursive function call
test_list = [5, 6, [], 3, [], [], 9]
# printing original list
print("The original list is : " + str(test_list))
result = remove_empty(0, test_list, [])
# printing result
print("List after empty list removal : ", result)
#Output : The original list is : [5, 6, [], 3, [], [], 9]
[END] |
Remove empty List from List | https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar | # Python3 Code to Demonstrate Remove empty List
# from List
import itertools
# Initializing list by custom values
test_list = [5, 6, [], 3, [], [], 9]
# Printing original list
print("The original list is : " + str(test_list))
# Removing empty List from List
# using lambda function
res = list(itertools.filterfalse(lambda x: x == [], test_list))
# Printing the resultant list
print("List after empty list removal : " + str(res)) |
#Output : The original list is : [5, 6, [], 3, [], [], 9] | Remove empty List from List
# Python3 Code to Demonstrate Remove empty List
# from List
import itertools
# Initializing list by custom values
test_list = [5, 6, [], 3, [], [], 9]
# Printing original list
print("The original list is : " + str(test_list))
# Removing empty List from List
# using lambda function
res = list(itertools.filterfalse(lambda x: x == [], test_list))
# Printing the resultant list
print("List after empty list removal : " + str(res))
#Output : The original list is : [5, 6, [], 3, [], [], 9]
[END] |
Remove empty List from List | https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar | # Initializing list
test_list = [5, 6, [], 3, [], [], 9]
# printing original list
print("The original list is : " + str(test_list))
# Remove empty List from List
# using map() function
res = list(map(lambda x: x if x != [] else None, test_list))
res = [x for x in res if x != None]
# printing result
print("List after empty list removal : " + str(res))
# This code is contributed by Vinay Pinjala. |
#Output : The original list is : [5, 6, [], 3, [], [], 9] | Remove empty List from List
# Initializing list
test_list = [5, 6, [], 3, [], [], 9]
# printing original list
print("The original list is : " + str(test_list))
# Remove empty List from List
# using map() function
res = list(map(lambda x: x if x != [] else None, test_list))
res = [x for x in res if x != None]
# printing result
print("List after empty list removal : " + str(res))
# This code is contributed by Vinay Pinjala.
#Output : The original list is : [5, 6, [], 3, [], [], 9]
[END] |
Remove empty List from List | https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar | import re
# input list values
input_list = [5, 6, [], 3, [], [], 9]
# print initial list values
print(f"The original list is : {input_list}")
# removing empty list from list
res = list(filter(None, [x for x in input_list if not re.match("\[\]", str(x))]))
# print resultant list
print(f"List after empty list removal : {res}") |
#Output : The original list is : [5, 6, [], 3, [], [], 9] | Remove empty List from List
import re
# input list values
input_list = [5, 6, [], 3, [], [], 9]
# print initial list values
print(f"The original list is : {input_list}")
# removing empty list from list
res = list(filter(None, [x for x in input_list if not re.match("\[\]", str(x))]))
# print resultant list
print(f"List after empty list removal : {res}")
#Output : The original list is : [5, 6, [], 3, [], [], 9]
[END] |
Remove empty List from List | https://www.geeksforgeeks.org/python-remove-empty-list-from-list/?ref=leftbar-rightbar | # Define the test list
test_list = [5, 6, [], 3, [], [], 9]
# Print the original list
print("The original list is : " + str(test_list))
# Counter variable 'i' to keep track of the current index
i = 0
# While loop to go through all elements of the list
while i < len(test_list):
# If the current element is an empty list, remove it from the list
if test_list[i] == []:
test_list.pop(i)
# Else, increment the counter variable
else:
i += 1
# Reassign the result to the original list after removing all empty lists
res = test_list
# Print the result
print("List after empty list removal : " + str(res))
# This code is contributed by Jyothi pinjala. |
#Output : The original list is : [5, 6, [], 3, [], [], 9] | Remove empty List from List
# Define the test list
test_list = [5, 6, [], 3, [], [], 9]
# Print the original list
print("The original list is : " + str(test_list))
# Counter variable 'i' to keep track of the current index
i = 0
# While loop to go through all elements of the list
while i < len(test_list):
# If the current element is an empty list, remove it from the list
if test_list[i] == []:
test_list.pop(i)
# Else, increment the counter variable
else:
i += 1
# Reassign the result to the original list after removing all empty lists
res = test_list
# Print the result
print("List after empty list removal : " + str(res))
# This code is contributed by Jyothi pinjala.
#Output : The original list is : [5, 6, [], 3, [], [], 9]
[END] |
Python - Convert List to List of dictionaries | https://www.geeksforgeeks.org/python-convert-list-to-list-of-dictionaries/ | # Python3 code to demonstrate working of
# Convert List to List of dictionaries
# Using dictionary comprehension + loop
# initializing lists
test_list = ["Gfg", 3, "is", 8, "Best", 10, "for", 18, "Geeks", 33]
# printing original list
print("The original list : " + str(test_list))
# initializing key list
key_list = ["name", "number"]
# loop to iterate through elements
# using dictionary comprehension
# for dictionary construction
n = len(test_list)
res = []
for idx in range(0, n, 2):
res.append({key_list[0]: test_list[idx], key_list[1]: test_list[idx + 1]})
# printing result
print("The constructed dictionary list : " + str(res)) |
#Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}] | Python - Convert List to List of dictionaries
# Python3 code to demonstrate working of
# Convert List to List of dictionaries
# Using dictionary comprehension + loop
# initializing lists
test_list = ["Gfg", 3, "is", 8, "Best", 10, "for", 18, "Geeks", 33]
# printing original list
print("The original list : " + str(test_list))
# initializing key list
key_list = ["name", "number"]
# loop to iterate through elements
# using dictionary comprehension
# for dictionary construction
n = len(test_list)
res = []
for idx in range(0, n, 2):
res.append({key_list[0]: test_list[idx], key_list[1]: test_list[idx + 1]})
# printing result
print("The constructed dictionary list : " + str(res))
#Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}]
[END] |
Python - Convert List to List of dictionaries | https://www.geeksforgeeks.org/python-convert-list-to-list-of-dictionaries/ | # Python3 code to demonstrate working of
# Convert List to List of dictionaries
# Using zip() + list comprehension
# initializing lists
test_list = ["Gfg", 3, "is", 8, "Best", 10, "for", 18, "Geeks", 33]
# printing original list
print("The original list : " + str(test_list))
# initializing key list
key_list = ["name", "number"]
# using list comprehension to perform as shorthand
n = len(test_list)
res = [
{key_list[0]: test_list[idx], key_list[1]: test_list[idx + 1]}
for idx in range(0, n, 2)
]
# printing result
print("The constructed dictionary list : " + str(res)) |
#Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}] | Python - Convert List to List of dictionaries
# Python3 code to demonstrate working of
# Convert List to List of dictionaries
# Using zip() + list comprehension
# initializing lists
test_list = ["Gfg", 3, "is", 8, "Best", 10, "for", 18, "Geeks", 33]
# printing original list
print("The original list : " + str(test_list))
# initializing key list
key_list = ["name", "number"]
# using list comprehension to perform as shorthand
n = len(test_list)
res = [
{key_list[0]: test_list[idx], key_list[1]: test_list[idx + 1]}
for idx in range(0, n, 2)
]
# printing result
print("The constructed dictionary list : " + str(res))
#Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}]
[END] |
Python - Convert List to List of dictionaries | https://www.geeksforgeeks.org/python-convert-list-to-list-of-dictionaries/ | # initializing lists
test_list = ["Gfg", 3, "is", 8, "Best", 10, "for", 18, "Geeks", 33]
# initializing key list
key_list = ["name", "number"]
# using zip() function and dictionary comprehension
res = [
{key_list[i]: val for i, val in enumerate(pair)}
for pair in zip(test_list[::2], test_list[1::2])
]
# printing result
print("The constructed dictionary list : " + str(res)) |
#Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}] | Python - Convert List to List of dictionaries
# initializing lists
test_list = ["Gfg", 3, "is", 8, "Best", 10, "for", 18, "Geeks", 33]
# initializing key list
key_list = ["name", "number"]
# using zip() function and dictionary comprehension
res = [
{key_list[i]: val for i, val in enumerate(pair)}
for pair in zip(test_list[::2], test_list[1::2])
]
# printing result
print("The constructed dictionary list : " + str(res))
#Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}]
[END] |
Python - Convert List to List of dictionaries | https://www.geeksforgeeks.org/python-convert-list-to-list-of-dictionaries/ | # Python3 code to demonstrate working of
# Convert List to List of dictionaries
# Using groupby() function from itertools module
# import itertools module
import itertools
# initializing lists
test_list = ["Gfg", 3, "is", 8, "Best", 10, "for", 18, "Geeks", 33]
# printing original list
print("The original list : " + str(test_list))
# initializing key list
key_list = ["name", "number"]
# using groupby() function to group elements into pairs
res = []
for pair in zip(test_list[::2], test_list[1::2]):
res.append({key_list[0]: pair[0], key_list[1]: pair[1]})
# printing result
print("The constructed dictionary list : " + str(res)) |
#Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}] | Python - Convert List to List of dictionaries
# Python3 code to demonstrate working of
# Convert List to List of dictionaries
# Using groupby() function from itertools module
# import itertools module
import itertools
# initializing lists
test_list = ["Gfg", 3, "is", 8, "Best", 10, "for", 18, "Geeks", 33]
# printing original list
print("The original list : " + str(test_list))
# initializing key list
key_list = ["name", "number"]
# using groupby() function to group elements into pairs
res = []
for pair in zip(test_list[::2], test_list[1::2]):
res.append({key_list[0]: pair[0], key_list[1]: pair[1]})
# printing result
print("The constructed dictionary list : " + str(res))
#Output : The constructed dictionary list : [{'name': 'Gfg', 'number': 3}, {'name': 'is', 'number': 8}, {'name': 'Best', 'number': 10}, {'name': 'for', 'number': 18}, {'name': 'Geeks', 'number': 33}]
[END] |
Python - Convert Lists of List to Dictionary | https://www.geeksforgeeks.org/python-convert-lists-of-list-to-dictionary/ | # Python3 code to demonstrate working of
# Convert Lists of List to Dictionary
# Using loop
# initializing list
test_list = [["a", "b", 1, 2], ["c", "d", 3, 4], ["e", "f", 5, 6]]
# printing original list
print("The original list is : " + str(test_list))
# Convert Lists of List to Dictionary
# Using loop
res = dict()
for sub in test_list:
res[tuple(sub[:2])] = tuple(sub[2:])
# printing result
print("The mapped Dictionary : " + str(res)) |
#Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)} | Python - Convert Lists of List to Dictionary
# Python3 code to demonstrate working of
# Convert Lists of List to Dictionary
# Using loop
# initializing list
test_list = [["a", "b", 1, 2], ["c", "d", 3, 4], ["e", "f", 5, 6]]
# printing original list
print("The original list is : " + str(test_list))
# Convert Lists of List to Dictionary
# Using loop
res = dict()
for sub in test_list:
res[tuple(sub[:2])] = tuple(sub[2:])
# printing result
print("The mapped Dictionary : " + str(res))
#Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)}
[END] |
Python - Convert Lists of List to Dictionary | https://www.geeksforgeeks.org/python-convert-lists-of-list-to-dictionary/ | # Python3 code to demonstrate working of
# Convert Lists of List to Dictionary
# Using dictionary comprehension
# initializing list
test_list = [["a", "b", 1, 2], ["c", "d", 3, 4], ["e", "f", 5, 6]]
# printing original list
print("The original list is : " + str(test_list))
# Convert Lists of List to Dictionary
# Using dictionary comprehension
res = {tuple(sub[:2]): tuple(sub[2:]) for sub in test_list}
# printing result
print("The mapped Dictionary : " + str(res)) |
#Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)} | Python - Convert Lists of List to Dictionary
# Python3 code to demonstrate working of
# Convert Lists of List to Dictionary
# Using dictionary comprehension
# initializing list
test_list = [["a", "b", 1, 2], ["c", "d", 3, 4], ["e", "f", 5, 6]]
# printing original list
print("The original list is : " + str(test_list))
# Convert Lists of List to Dictionary
# Using dictionary comprehension
res = {tuple(sub[:2]): tuple(sub[2:]) for sub in test_list}
# printing result
print("The mapped Dictionary : " + str(res))
#Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)}
[END] |
Python - Convert Lists of List to Dictionary | https://www.geeksforgeeks.org/python-convert-lists-of-list-to-dictionary/ | original_list = [["a", "b", 1, 2], ["c", "d", 3, 4], ["e", "f", 5, 6]]
mapped_dict = {(lst[0], lst[1]): tuple(lst[2:]) for lst in original_list}
print("The mapped Dictionary :", mapped_dict) |
#Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)} | Python - Convert Lists of List to Dictionary
original_list = [["a", "b", 1, 2], ["c", "d", 3, 4], ["e", "f", 5, 6]]
mapped_dict = {(lst[0], lst[1]): tuple(lst[2:]) for lst in original_list}
print("The mapped Dictionary :", mapped_dict)
#Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)}
[END] |
Python - Convert Lists of List to Dictionary | https://www.geeksforgeeks.org/python-convert-lists-of-list-to-dictionary/ | # Python3 code to demonstrate working of
# Convert Lists of List to Dictionary
# Using zip() and loop
# initializing list
test_list = [["a", "b", 1, 2], ["c", "d", 3, 4], ["e", "f", 5, 6]]
# printing original list
print("The original list is : " + str(test_list))
# Convert Lists of List to Dictionary
# Using zip() and loop
result_dict = {}
for sublist in test_list:
key = tuple(sublist[:2])
value = tuple(sublist[2:])
result_dict[key] = value
# printing result
print("The mapped Dictionary : " + str(result_dict)) |
#Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)} | Python - Convert Lists of List to Dictionary
# Python3 code to demonstrate working of
# Convert Lists of List to Dictionary
# Using zip() and loop
# initializing list
test_list = [["a", "b", 1, 2], ["c", "d", 3, 4], ["e", "f", 5, 6]]
# printing original list
print("The original list is : " + str(test_list))
# Convert Lists of List to Dictionary
# Using zip() and loop
result_dict = {}
for sublist in test_list:
key = tuple(sublist[:2])
value = tuple(sublist[2:])
result_dict[key] = value
# printing result
print("The mapped Dictionary : " + str(result_dict))
#Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)}
[END] |
Python - Convert Lists of List to Dictionary | https://www.geeksforgeeks.org/python-convert-lists-of-list-to-dictionary/ | from functools import reduce
# initializing list
test_list = [["a", "b", 1, 2], ["c", "d", 3, 4], ["e", "f", 5, 6]]
# printing original list
print("The original list is: " + str(test_list))
# define function to combine dictionaries
def combine_dicts(dict1, dict2):
dict1.update(dict2)
return dict1
# use reduce to apply combine_dicts to all nested dictionaries
res = reduce(combine_dicts, [{tuple(sub[:2]): tuple(sub[2:])} for sub in test_list])
# print mapped dictionary
print("The mapped dictionary: " + str(res)) |
#Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)} | Python - Convert Lists of List to Dictionary
from functools import reduce
# initializing list
test_list = [["a", "b", 1, 2], ["c", "d", 3, 4], ["e", "f", 5, 6]]
# printing original list
print("The original list is: " + str(test_list))
# define function to combine dictionaries
def combine_dicts(dict1, dict2):
dict1.update(dict2)
return dict1
# use reduce to apply combine_dicts to all nested dictionaries
res = reduce(combine_dicts, [{tuple(sub[:2]): tuple(sub[2:])} for sub in test_list])
# print mapped dictionary
print("The mapped dictionary: " + str(res))
#Output : The mapped Dictionary : {('a', 'b'): (1, 2), ('c', 'd'): (3, 4), ('e', 'f'): (5, 6)}
[END] |