Hi,
I want to change an item from one value to another. How would I do this?
Hi,
I want to change an item from one value to another. How would I do this?
Here is a way
# python 2 and 3
def swap(a, i, j):
"swap items i and j in the list a"
a[i], a[j] = a[j], a[i]
if __name__ == "__main__":
L = list(range(0, 70, 10))
assert( L == [0, 10, 20, 30, 40, 50, 60] )
swap(L, 1, 4)
assert( L == [0, 40, 20, 30, 10, 50, 60] )
print("success: the swap function worked")
""" --> my output
success: the swap function worked
"""
Please, post code in this forum to illustrate your questions.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.