Hi so i want to add a variable (integer) to every value in my list, how can i do this? Thanks, heres my code,
mylist at the moment is [83, 111, 109, 101, 119, 104, 101, 114, 101, 105, 110, 108, 97, 77, 97, 110, 99, 104, 97, 44, 105, 110, 97, 112, 108, 97, 99, 101, 119, 104, 111, 115, 101, 110, 97, 109, 101, 73, 100, 111, 110, 111, 116, 99, 97, 114, 101, 116, 111, 114, 101, 109, 101, 109, 98, 101, 114, 44, 97, 103, 101, 110, 116, 108, 101, 109, 97, 110, 108, 105, 118, 101, 100, 110, 111, 116, 108, 111, 110, 103, 97, 103, 111, 44, 111, 110, 101, 111, 102, 116, 104, 111, 115, 101, 119, 104, 111, 104, 97, 115, 97, 108, 97, 110, 99, 101, 97, 110, 100, 97, 110, 99, 105, 101, 110, 116, 115, 104, 105, 101, 108, 100, 111, 110, 97, 115, 104, 101, 108, 102, 97, 110, 100, 107, 101, 101, 112, 115, 97, 115, 107, 105, 110, 110, 121, 110, 97, 103, 97, 110, 100, 97, 103, 114, 101, 121, 104, 111, 117, 110, 100, 102, 111, 114, 114, 97, 99, 105, 110, 103, 46]
and offset_number is 22
and lengthoflist is 171

``````      lengthoflist = len(mylist)
q = 0
while q < lengthoflist:
for i in mylist :
#adds every number to the offset factor
i = i + offset_number
q = q + 1
#if it is more than 126
if i > 126:
#take 94 away from it
i = i - 94

else:
#if it less than 126 g + 1
g = g + 1
print(mylist)
``````

Edited by pritaeas: Moved to Python

3
Contributors
2
Replies
49
Views
3 Years
Discussion Span
Last Post by Josiah_2

I don't understand what `q` and `g` are in your code. If you want to add a number to every value in a list, you can do

``````mylist = [83, 111, 109]
offset = 22
mylist[:] = [value + offset for value in mylist]
# mylist is now [105, 133, 131]
``````

I agree with Griboullis as that is an easy way of doing so but i think you left a bit of information out of your original post. Do you need it to add 22 to every value on your list unless that makes it greater than 126. If it is greater than 126 than you need to subtract 94 otherwise if it is less than add 1 to it and put it back in the list?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.