1

I am trying to make a new list, which is a replica of an other list. This new list I want to modify by changing values at specific positions.

I found this question, and the most upvoted answer can make a new list and add on to the old list. This is a good start, but I want to decide where the additions are done (in his version, it just adds it on to the end of the list)

What he has:

myList = [10,20,30] 

yourList = myList + [40]

print("your list: " + yourList) 

your list: [10,20,30,40]

In this instance, I would want to be able to change where the 40 in your list goes ie

your list: [10,40,20,30]

I'm unsure how I would go about doing that. Thanks! (Pretty new here sorry if I'm not clear)

rdas
  • 20,604
  • 6
  • 33
  • 46
Mr Nielsen
  • 11
  • 4

3 Answers3

1

Use slicing to split the original list apart at the desired index, then put it back together again.

For example:

insert_at = 1

myList = [10,20,30] 

# Uses PEP 448 unpacking generalizations to avoid excess temporary `list`s
yourList = [*myList[:insert_at], 40, *my_list[insert_at:]]

# Or via concatenation (involves an extra temporary)
yourList = myList[:insert_at] + [40] + my_list[insert_at:]

print("your list: " + yourList) 

Alternatively, if you don't need a one-liner, copy the list, then call insert on the copy:

yourList = myList[:]  # myList.copy() also works on Python 3
yourList.insert(insert_at, 40)
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
0

To keep the same list:

Use the insert method of the list class

myList.insert(1, 40)

insert(index, element) inserts the element into the list at the position index - shifting the remaining elements rightward as required.

To make a new list:

new_list = myList.copy()
new_list.insert(1, 40)
rdas
  • 20,604
  • 6
  • 33
  • 46
  • They seem to want to make a new `list`; `insert` modifies in place. You'd need to explicitly copy (with `myList[:]` or `myList.copy()`), then call `insert` on the copy to make this work. – ShadowRanger Jun 12 '19 at 02:34
  • That's going to change the original list though, I want to make a new list and keep the old one the same. I tried to do yourList = myList.insert(1, 40) and it just returns None (I'm pretty sure that means I can't make a new list with it then?) – Mr Nielsen Jun 12 '19 at 02:34
  • @MrNielsen: That's a Python standard behavior; mutating methods always return `None` (or something other than the instance they were called on when appropriate, e.g. `dict.setdefault` and `dict.pop` return the value in question), so you're not deceived into thinking it made a modified copy. – ShadowRanger Jun 12 '19 at 02:35
  • @ShadowRanger That's exactly what I want. How do I go about copying the list though? I can't make yourList = myList because as soon as I change yourList it's also going to also change myList, but I want the old lists to stay the same. – Mr Nielsen Jun 12 '19 at 02:37
  • @MrNielsen I've edited my answer to include how you can make a copy of the list (in python 3) – rdas Jun 12 '19 at 02:38
  • @MrNielsen: Read [my answer](https://stackoverflow.com/a/56553895/364696); it includes the necessary copying component (either the complete slice, `[:]` or the `copy()` method). – ShadowRanger Jun 12 '19 at 02:38
  • Thanks so much, @ShadowRanger and rdas the .copy() works great for what I'm trying to do – Mr Nielsen Jun 12 '19 at 02:42
0

you can copy your list and insert the value in the new list something like this:

my_list = [1,2,3,4,5,6]
my_2_list = my_list.copy()

then i wnat to insert a vlue at position 3 i can do

my_2_list.insert(3, value)
juancarlos
  • 593
  • 3
  • 9