Which data structure preserves order in python? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am looking for a data structure which allows fast search and also preserves the order of the data it received?

Lists and tuples preserve order, dictionaries and set don't. An available ordered version of a dictionary is collections.OrderedDict, which provides a faster search than a list.

Take a look at this
You have dictionaries and set:(unordered)
Dictionary is an unordered set of key: value pairs
set is an unordered collection with no duplicate elements
You can either use for preserving the order:
list
tuple
collections.OrderedDict
Choosing the data structure totally depends on your requirement.
List and tuple hold a basic difference. List is mutable and tuples is immutable
ordered dictionary is nothing but a key value pair.
So you can choose accordingly!

Related

When to use Lists, Sets, Dictionaries, or tuples in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm new to python, and I'm a bit confused about the use cases of data types in python
Can someone please explain in detail when to use each data type, with an example if possible
Thank you.
Lists are used when you have data you want to further modify, alter like sorting and all.
Dictionary is used when you have to sets of data where data of the first set corresponds to data of other set. And the position of the data doesn't matter only the relation of the two sets matters.
A tuple is used when position of the data is very important and you don't want to alter the position throughout.

Python: what is the best way to store numerical key value pairs? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have data which consist of numerical keys and values.
I need to increase all keys and values by N number.
While i am using dictionaries for big amout of data my code works very slow.
What is the best way to store this data and the best way to increase values of pairs?
Example:
N=2
{1:4,3:6,2:1}
expected result:
{3:6,5:8,4:2}
Thanks
We can not actually do something faster if you want to change the whole data of dictionary. Even If someone run a for loop we are not sure of O(N) complexity because there can be re-hashing operations internally.
Best thing is you can smartly use one extra variable in memory for updates.
Like initially
del=0 and d={1:4,3:6,2:1}
when you want to increase values and keys by N
update del+=N
While retrieving from dictionary for key value k
Use d[k-del]+del
Best you can do about this is O(N) whichever data structure you use, you will have to visit the values of each element and increment them.

set function usage? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Is it a good practice to use set function to list all the unique elements or are there any better approaches which have low time and space complexity.
Simply calling set is the best way to find the unique elements if:
the items are hashable, and
you don't require the original ordering preserved
It's already O(n), and you can't improve on that asymptotically. If you require the ordering preserved:
from collections import OrderedDict
list(OrderedDict.fromkeys(seq))
That's still O(n) asymptotically, but will generally slower due to more overhead (Python loop vs C loop). If you have to deal with unhashable elements, you may need O(n^2) using a list:
unique = []
for item in seq:
if item not in unique:
unique.append(item)

How to represent "range" in python dict [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to create dictionary like this:
1,2 = "A"
3,4 = "B"
5,6,7・・・(>=5) = "C"
so I tried below, but didnt works well..
dict = {1:"A", 2:"A", 3:"B", 4:"B", >=5:"C"}
how can I create a dictionary like this?
A lookup of a value where the key is in a sorted range is capped at O(log n). Hence using a dictionary directly is not viable. You can either do this by:
storing the ranges in list, sorting them, and doing a binary search, or...
using a tree data structure.
There are other creative solutions such as storing all the numbers in the range in the dictionary as integer keys and have duplicate references to the same value (which looks like what you have above). This would be O(1) lookup, but would have the negative side effects such as:
cannot modify the ranges easily without updating all the keys
wasted space for large ranges
cannot do lookups with floats
...which might be okay depending on your use case.
In summary, there is no direct solution.

What is the definition of a 'list' in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I always use lists when coding but can never put into words when analyzing my own work. what is the definition of a list in detail?
It's a datatype that stores a bunch of stuff. The more general term is an 'array.'
In python, they are represented like so:
[1, 2, 3, 4, 'elem5', ['sub-list-item-1', 'sub-list-item-2'], {}]
One important aspect of lists is that they can contain any type of data, or mixed data types.
Python docs define a list thusly:
list
A built-in Python sequence. Despite its name it is more akin to
an array in other languages than to a linked list since access to
elements are O(1).
Read More Here

Categories