Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
swap elements in list
#1
Hi all

So the only way i can think of to swap two elements in list with the following

def swap(arr, pos1, pos2):
I wonder if there is away where I dont have to send the position of elements to be swapped. Instead by sending references to those elements in list. Like what we do in c or c++. I cant get my head around how the references work in this case in python.

Many thanks
Reply
#2
>>> mylist = [1, 2, 3, 'hello', 'goodbye', 'Saturday']
>>> mylist
[1, 2, 3, 'hello', 'goodbye', 'Saturday']
>>> mylist[3], mylist[2] = mylist[2], mylist[3]
>>> mylist
[1, 2, 'hello', 3, 'goodbye', 'Saturday']
>>>
Reply
#3
So, you question is about the following: If I have memory addresses of item[i] and item[j] (alist = [item[0], ... , item[N]], can I swap items without calling __getitem__, e.g. without accessing items via [ ... ]?. You are likely can do this using ctypes module, probably by means of ctypes.memmove function and/or sys.getsizeof function.
I think, the following code snippet could help you to write a swapper what you want.
Note, in this case you probably will need to take into account types of list elements, take care about references and internal structure of underlying PyObjects. So, this way is unstable and probably will lead to crash the Python interpreter.

Another approach could be much simpler, you can write a help function that returns index of the element by its address (get_index(address = id(item[i]))). When indices of elements are known, they can be easily swapped as @Larz60+ pointed out.
Reply
#4
my_items = [1, 2, 3, 4, 5]


def swap(arr, item1, item2):
    index1 = my_items.index(item1)
    index2 = my_items.index(item2)
    arr[index1], arr[index2] = arr[index2], arr[index1]

    
swap(my_items, 2, 5)
print(my_items)
Output:
[1, 5, 3, 4, 2]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 377 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Checking if a string contains all or any elements of a list k1llcod3 1 1,023 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 1,911 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,013 May-17-2022, 11:38 AM
Last Post: Larz60+
  How to swap two numbers in fields in python Joni_Engr 5 1,804 Jan-11-2022, 09:43 AM
Last Post: menator01
  Why am I getting list elements < 0 ? Mark17 8 3,032 Aug-26-2021, 09:31 AM
Last Post: naughtyCat
  Looping through nested elements and updating the original list Alex_James 3 2,070 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Extracting Elements From A Website List knight2000 2 2,182 Jul-20-2021, 10:38 AM
Last Post: knight2000
  Make Groups with the List Elements quest 2 1,936 Jul-11-2021, 09:58 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020