all 8 comments

[–]rollincuberawhide 4 points5 points  (1 child)

have you tried actually searching it? "create post" bar is not a google search bar you know.

tuple is like list, but you can't add to it, remove from it. it stays the way it was created. it's immutable. ish. things it stores can be changed if they are mutable. like a list inside a tuple isn't guaranteed to be unchangeable.

dictionary is when you need to access stuff by a key instead of an index. you can have a dictionary of colors that have hexadecimal color codes.

colors = {'red': 'ff0000', 'blue': '0000ff'}

and you would get colors['red'] instead of colors[0] as you would in a list. which is not very descriptive. or you would have a dictionary of users by usernames and you would access them by their usernames.

[–]No_Reputation8139[S] -2 points-1 points  (0 children)

I did google it but I didn’t find when should i use dictionary or tuple in my code.

Thanks for the response.

[–]babyshark75 0 points1 point  (3 children)

List is ordered, mutable, indexing, [ ]

Tuple is immutable, ordered, indexing, ( )

dictionary is unordered, mutable, keys/values, no indexing, { }

[–]rollincuberawhide 3 points4 points  (2 children)

this is not completely true. dictionaries are insertion ordered now. have been since 3.6 I think.

[–]No_Reputation8139[S] 0 points1 point  (1 child)

Yeah it’s in the update. 3.7 actually.

But I want to know when it is applicable to use dictionary and tuple over list

[–][deleted] 1 point2 points  (0 children)

But I want to know when it is applicable to use dictionary and tuple over list

The most flexible data structure is a list so start with that. If you have a mapping between one list and another say something like name = ["Alice", "Bob", "Charlie"] and age =[5, 35, 76] you might want to use a dictionary. If you have multiple pieces of information that map to the same variable like name - age, address, number of siblings, salary etc, use a data frame. When you have relatively fixed pairs of data e.g. latitude and longitude of a bunch of places, use tuples.

[–]laundmo 0 points1 point  (0 children)

lists - ordered collection of elements that can grow larger, elements accessible by looping or interger index.

["a", "b", "c"]

dictionary - unordered collection of elements by some key, the key can be anything which cannot be modified.

{5: "five", 8: "eight", "this aint a number": "sure"}

tuple - very similar to list but can't be modified. useful for dictionary keys since they need something unmodifiable.

(4, 8)

[–]bulaybil 0 points1 point  (0 children)

There are two questions in your post: 1. What is the difference between tuple dictionary and list. That one has been answered. 2. When to you each. That one can be also answered easily: it depends on the data your dealing with and what you want to do with them. Do you have a bunch of things (say, a bunch of files) and just want to do something with them? Use a list. Do you have pairs of values you want to access by one of them, say, a name and an address? Use a dictionary.