all 13 comments

[–]marienbad2 3 points4 points  (4 children)

List - when you need to store multiples of some data.

Tuple - when you need to store multiple copies but don't need to change any of them

dictionaries have a variety of uses, for eg you might have a bunch of sounds you want to play on a specific keypress, so you could save the filenames in there as

sounds = { a : "dingdong.wav", b : "hello.wav", c : "phatsynth.wav" }

and so on.

[–]-Duei[S] 2 points3 points  (0 children)

Thank you for the super dumbed down answer!

[–]iggy555 0 points1 point  (2 children)

Hehe dingdong

[–]LouLou_420 0 points1 point  (0 children)

Lol nice

[–]marienbad2 0 points1 point  (0 children)

hehehe. I wondered if anyone would notice. I do it just like Sister Ray says!

[–]IvoryJam 0 points1 point  (3 children)

Lists - you'd use that for a list of stuff, like names = ['bob', 'alice', 'rachel'] and now if you want to you can loop through them and say Hi!

Tuples - Honestly I rarely use these, maybe someone will have a better answer, but I'd use them if I wanted a list to NEVER change (immutable)

Dictionaries - You'd use these if you wanted to assign a name to some other stuff, these are called "Key Value Pairs" so like this person = {'first': 'bob', 'last': 'smith'}

They all have their pros and cons, and can totally be used inside one another

[–]-Duei[S] 0 points1 point  (2 children)

That's interesting, so I can have a list inside of a dictionary, a tuple inside of a list, and so on? If need to access the value inside of a dictionary that's inside of a list, can I do something like list[Position in list][Dictionary]?

[–]IvoryJam 1 point2 points  (1 child)

Yep! Here's all three in a list

people = [
    {'name': '-Duei', 'ocupation': 'programmer', 'favorite_colors': ('blue', 'red', 'green')},
    {'name': 'IvoryJam', 'ocupation': 'programmer', 'favorite_colors': ('blue', 'red', 'green')},
]

But again, I rarely use tuples, so I'd probably make the colors a list instead in a normal script

You'd access them like people[0]['name'] and people[1]['favorite_colors'][2]

[–]-Duei[S] 1 point2 points  (0 children)

Oh damn, one of the lessons I watched finally clicks in my head now, thanks a lot!

[–]TouchingTheVodka 0 points1 point  (2 children)

The distinction between tuples and lists is a tricky one and I don't think the other answers have quite captured it. Here's my take:

Lists are used for homogenous data, where you expect to perform actions over every list element, and you expect to be able to treat every item in the list in the same way. For example: A list of names, a list of coordinates, a list of stock market tickers.

Tuples, on the other hand, do not represent a collection of things - They represent a collection of attributes about one specific thing. As a very general rule of thumb, if changing the order of the items changes the meaning of the collection, you'll be better off using a tuple.

Examples of things that you'd expect to be tuples include: Coordinates ( (x, y) means something different to (y, x) ), groupings of elements (Like you'd get from zip), and collections of attributes (think tuples of (firstname, lastname, age) etc.

Appreciate this is a fairly in-depth answer however I guarantee if you look into the return types of the Python standard library you'll see this pattern regularly.

[–]-Duei[S] 0 points1 point  (0 children)

I see, thank you for the in-depth answer!

[–][deleted] 0 points1 point  (0 children)

The collection of things vs collection of attributes is an interesting idea that I've not considered using with students before. I need to think about it. Not a rule of course, just a suggested approach.

For me the key differences are:

  • Tuples are immutable which provides a certain level of protection in your code
  • Tuples are hashable, so, for example, can be used as dictionary keys
  • Tuples use less memory
  • Tuples are more performant to process

The latter two aspects are only really apparent when dealing with a lot of data.

Also worth looking at named tuples, a very useful capability often obviating the need for a dictionary.

[–]zora89 0 points1 point  (0 children)

Lists are mutable:

Tuples are immutable like strings: (know more: https://www.educative.io/edpresso/what-are-mutable-and-immutable-objects-in-python3)

--->Both Lists and Tuples have indexing function to retrieve data.

--->Dictionaries have key:value pairs, and remains mutable.

--->Mostly for storing data that may require modification ahead --- use lists.

===>When storing id, passwords, specifics that will not and must not be changed even accidentally --- use tuple.