-1

i'm a beginner on Python and what i'm trying to do is adding an element in a specific index in a list of values associated to my key in Python 3, using defaultdict.

My default_dict structure is the following:

default_dict = ['label_1':[list of doubles], 'label_2':[list of doubles], etc..]

suppose i have a double d = 6.5 and i want to insert it as a value of the list with key: label_1 in a specific index.

if my list is:[12.3, 11.8, 8.6, 5.8, 3.1]

i would like to insert my d = 6.5 between 8.6 and 5.8

i know a call like default_dict[label_1].append(d) would result in a list like that:

[12.3, 11.8, 8.6, 5.8, 3.1, 6.5]

is there a function i can use like: default_dict[label_1].add(d, index_to_insert) ?

i thought i could fetch the entire list, modify it by myself and switch the new one with the old i had but i think this is a onerous call (i need to call that a lot of time) and i need a more efficient way!

Thanks in advance!

Basionkler
  • 13
  • 7
  • 2
    Does `default_dict['label_1'].insert(3, 6.5)` do what you want? It's unclear if you're trying to insert the value at a specific index or between two specific values (like 8.6 and 5.8). – Aran-Fey Mar 27 '18 at 16:41
  • 2
    You couldn't find documentation on how to insert a value into a list? – Prune Mar 27 '18 at 16:43
  • @Aran-Fey i'm trying to build an ordered list of double from the higher to the lower, without using sorting functions. Every time i get a value i check the list and i insert thi value to the index it belongs. In my example 6.5 is between 8.6 and 5.8 becase, 6.5 is smaller than 8.6 and greater than 5.8. Index changes every time. Value changes every time. – Basionkler Mar 27 '18 at 16:52
  • @Prune if i was successfull on finding a doc explaining what i need, i wouldn't ask that on SO ;) – Basionkler Mar 27 '18 at 16:54
  • Okay, so which question are we supposed to answer? "How do I insert a value at a specific index?" is a different question than "How do I insert a value into a sorted list so that the list remains sorted?" – Aran-Fey Mar 27 '18 at 16:58
  • @Aran-Fey Nevermind. Someone else understood my problem, thanks again for your time. – Basionkler Mar 27 '18 at 17:01
  • 2
    ...that's not how it works. You can't just dismiss the question just because someone else understood what you meant. StackOverflow questions have to _clear_ and _useful_. If the reader can't tell what your question is, then the question is not of lasting value and should be closed/deleted. – Aran-Fey Mar 27 '18 at 17:04

1 Answers1

2

Use list.insert(index,object) for adding into specific index of List.

Please note you have to do this for inside list you have in default_dict as you are referring.

ex:

>>li1 = [1,2,3,4,5]
>>li1.insert(5,8)
>>li1
[1, 2, 3, 4, 5, 8]
>>li1.insert(1,99)
>>li1
[1, 99, 2, 3, 4, 5, 8]

More efficient way is slicing too, if you are inserting single or multiple elements at specific index without replacing existing values.

>>li1 = [1,2,3,9,6,4,9,0]
>>li1[3:3] = [7]
>>li1
[1, 2, 3, 7, 9, 6, 4, 9, 0]

Please note time complexity for insert is O(n) and slicing is O(k)

Morse
  • 8,258
  • 7
  • 39
  • 64
  • 1
    Maybe worth noting that that is a `O(n)` operation in the worst case, whereas `append` is at worst `O(1)`. – Graipher Mar 27 '18 at 16:54
  • yes, good point. OP wants to insert in specific index, do we have anything thats like `log(n)`? – Morse Mar 27 '18 at 16:55
  • No. Not if your datastructure is a normal Python `list`. – Graipher Mar 27 '18 at 16:58
  • how about [slicing](https://codereview.stackexchange.com/questions/83338/insert-elements-into-a-list)? I think thats more efficient than insert though. `O(k)` – Morse Mar 27 '18 at 16:59
  • @Prateek Thanks for your reply, does the side-effect on the list take effect on default_dict? i mean, if i save the values-list from the dictionary and than i modify the list i got, shouldn't i replace the old list (still on the dictionary) with the new one? – Basionkler Mar 27 '18 at 17:08
  • 1
    @Prateek But then you need to do `li1[:i] + [8] + li1[i:]`. The two slices are already `O(k+l)`, with `k+l = n`. And the list addition might at least double that again because it needs to create two new lists (allocating memory for them and all). – Graipher Mar 27 '18 at 17:09
  • 1
    @Basionkler `list.insert` is a method that modifies the list instance in place. You do not need to re-assign it (in fact, if you do, it will be `None`, because methods that modify an object in place return `None` by convention, and not a copy of the modified object). – Graipher Mar 27 '18 at 17:11
  • @Graipher thanks, yeah slicing seems `O(2k)` in this case one cant expect any better I guess than these two. – Morse Mar 27 '18 at 17:13
  • 1
    One thing that can work if you have many insertions is `append` them all and then in the end `sort` the list (because it seems like OP wants to insert the element at the position where it would go if the list needs to stay sorted). That would be a single `O(n*log(n))`, instead of `k*O(n)` for `k` insertions. But in that case you probably want to use a min heap anyways. – Graipher Mar 27 '18 at 17:14
  • @Graipher thanks, i'm going to try both of them and see which one goes faster, i thought sorting function would take much time but maybe i was totally wrong :) – Basionkler Mar 27 '18 at 17:24