replace values from list. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

replace values from list.

# Input : [2,1,2,3,0,2] and 3 # Output : [33,3,33,333,0,33] n = 3 Output = [ ] for item in input: Output.append(int(str(item)*n)) print (Output)

13th Apr 2022, 9:05 AM
Manoj Bhaagam
4 Answers
+ 3
Assuming the list is named <lst> and the number is <num> - Read <num> as string - Iterate through the items in list <lst> using enumerate() function - For each element <el> in iteration ... - Create a new string <res> a result from multiplying <num> by <el> - Convert <res> to integer by int() if you want the list items as numbers - Update <el> value by <res> Go give it a try 👍 You can also use list comprehension to accomplish this
13th Apr 2022, 9:28 AM
Ipang
+ 3
Manoj Bhaagam, That was pretty close 👍 lst = [ 2, 1, 2, 3, 0, 2 ] n = 3 Output = [] for item in lst: if item != 0: Output.append( int( str( n ) * item ) ) else: Output.append( item ) print(Output)
13th Apr 2022, 9:52 AM
Ipang
+ 2
numlist = [2,1,2,3,0,2] n = 3 multiple = [int(i*str(n)) if i else i for i in numlist] print(multiple)
13th Apr 2022, 12:02 PM
Simba
Simba - avatar
0
Where's your try?
13th Apr 2022, 9:18 AM
JOKER
JOKER - avatar