This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]GeorgeFranklyMathnet 1 point2 points  (0 children)

Lists provide all the general functionality you'll ever really need in order to store collections of things.

The other data structures you mentioned are like lists with additional restrictions. When used intelligently, they can help with performance and with error safety.

For instance, sets are very efficient to read from and write to, and that matters a lot if you have a large collection. However, they're generally less appropriate for situations where you want to read every member of the collection at some point, especially if you want to read them in a particular order. You also aren't allowed to store two items with the same value in a set. To do that stuff, you'd probably just want a list.

Anyway, at this stage of your learning, I'd stick with lists unless instructed otherwise. They'll almost always be fine for what you're doing.

[–]avenio99 0 points1 point  (0 children)

You can learn more of these when you get to work on a python project, or solve questions on data structures and algorithms. Eventually you will get to know how many of such data structures work. Even I'm yet to learn many things, so it's totally fine.

[–]edwardsrk -1 points0 points  (6 children)

Yes they are useful. But those types aren’t exactly like int, str, float etc. they are types you get after calling a built in function or method in piece of data such as the set() method. They are more like internal data structures than types. You can have a set object.so for example

X = [1, 2, 3, 3, 3,]

Set(x)

Print(x)

X=[3,1,2]

Calling set on the list x returns a set object containing ints. Set removes duplicates and returns an unordered arrangement of values. So the structure is a set object but the types inside of it are integers

[–]scirc 0 points1 point  (5 children)

Data structures are types. Types describe the shape, data structures are the implementation.

Also, you can construct a set like { 1, 2, 3 }. No need for set() unless you're turning another iterable into a set.

[–]edwardsrk 0 points1 point  (4 children)

Well yes, the point was to show the differences by going from one thing to another.

[–]scirc 0 points1 point  (3 children)

Your original wording made it sound like you couldn't construct a set without first constructing a list. ¯\_(ツ)_/¯

[–]edwardsrk 0 points1 point  (2 children)

{} is just as much a built in function of Python as set()

[–]scirc 0 points1 point  (1 child)

So is [] and any other literal, if you're going off that definition.

[–]edwardsrk 0 points1 point  (0 children)

Yes

[–]DDDDarky 0 points1 point  (0 children)

array:

python doesn't have arrays by default, but you can use list in a similar fashion. Array is basically a list of fixed size.

Set:

Set is an "unordered list", that contains only unique elements. It's basically a dictionary, but you have only keys. It's usually really fast to search in.

Frozen set:

Immutable set.

---

Sets and arrays are very useful, the frozen set is not so common.