Python Forum
modifying a list in an expression
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
modifying a list in an expression
#1
i have some incoming data. each unit of data can be converted to a list. that list can be converted back to that data. the conversions are expressions. i can nest these two expression to convert the data to a list and then back. i have a modification to the data that is defined in terms of it being in the list form. the modification is copying element index B to element index A of the list like. i can do this in 3 lines:

    temp = convert_data_to_list(data)
    temp[A] = temp[B]
    data = convert_list_to_data(temp)
is there an easy way to use slicing (or something else) to make this all be a single expression?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Something like this (as you can see it's readability is not great)?

convert_list_to_data([convert_data_to_list(data)[B] if convert_data_to_list(data).index(x) == A else x for x in convert_data_to_list(data)])
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
that's also going to be slow. the code i gave is actually going to be in a loop affecting many units of data (each unit can be converted to a list). most units are strings and conversion to a list was a workaround because they are immutable. i guess squeezing this to a big expression in one line is just not going to happen.

and, yeah, it is harder to read. so i guess it's back to separate steps.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,660 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Modifying code cheburashka 1 1,290 Dec-13-2021, 01:01 PM
Last Post: Kebap
  Regular expression: return string, not list Pavel_47 3 2,490 Jan-14-2021, 11:49 AM
Last Post: Pavel_47
  Pass results of expression to another expression cmdr_eggplant 2 2,278 Mar-26-2020, 06:59 AM
Last Post: ndc85430
Question Why does modifying a list in a for loop not seem to work? umut3806 2 2,293 Jul-22-2019, 08:25 PM
Last Post: umut3806
  iterate through a list with comparison of expression Alexandro 6 3,410 Jan-31-2019, 05:16 PM
Last Post: Scorpio
  Is there a way to append to a list WITHOUT modifying the parameter passed? arnavb 11 5,693 Sep-23-2018, 07:16 AM
Last Post: Skaperen
  Modifying / extracting multiple list items simultaneously using variable from range ehammarlund 4 3,686 Dec-06-2017, 08:15 PM
Last Post: ehammarlund
  Modifying Lists RedSkeleton007 12 6,805 Nov-22-2017, 04:31 AM
Last Post: heiner55
  list comprehension with regular expression aibrain 1 4,455 May-22-2017, 11:41 AM
Last Post: zivoni

Forum Jump:

User Panel Messages

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