r/learnpython • • Jul 03 '20

In the most basic terms, please explain when you would use tuples,lists,dictionaries, or sets.

Hello, I have been rereading and seeking different definitions and watching different videos explaining python, but I cannot understand what the difference is between tuples,lists, dictionaries, and sets other than some you use {} [] or (). When would you use these? What are the practical differences? please explain in the absolute simplest caveman terms

Edit: Thank you all for your generosity of knowledge. I have learned alot from reading all the comments and answers.

326 Upvotes

61 comments sorted by

364

u/ka-splam Jul 03 '20 edited Jul 03 '20

They are all ways of storing and accessing data, so very similar in that sense. What's different is the things they focus on, and the things they do faster or slower.

Lists are the simple way to hold many things. They can hold [1,2,3,4,3,2,1], almost as much as you like, you can add more things, remove things, change things. And you can pull things out by position or one at a time from beginning to end, or from a few positions at once. Good place to hold all kinds of things - lines of text from a file, user accounts, websites to scrape, filenames from a folder, whatever you're working with will probably go in a list at some point. Almost whatever, whenever. The default thing to use.

Sets are like the mathematical sets - they can't contain duplicates, they would see that list above as {1,2,3,4} only the unique members. This is good for doing math-set things like Venn diagrams show circles overlapping - "things which are in this set, AND in that set", or "things in that set and NOT in this set". Union, intersection. They tend to have a fast check for "is something in the set?", faster than you can check if something is in a list. If you're doing that a lot, it makes sense. They're a bit specialised.

Tuples are a more limited list, they can't grow or shrink, or have items changed or replaced, which makes them less useful, but they have less overhead so they run a bit faster. Good if you're doing something like pairs of items and you know there will always be two, or dealing with images and you have (Red, Green, Blue) colours going together, say. Usually used without paying much attention to it in things like for index, value in enumerate(x): there's a tuple in there around the comma, no () needed.

Dictionaries are wildly different, they're a way to store things tagged by a name (a key). Then you can lookup the tag and get the things back. E.g. lookup user ID, get email address back. They are extremely quick at finding things by name, and mapping key -> value is very useful in a ton of programming situations.

one example use, web scraping:

  • you make a list of website addresses to scrape ['http://example.com', 'http://google.com']

  • as you scrape them and get web page content back, you need somewhere to put that

  • you make a dictionary of {address: content} lookups. {'http://example.com' : "<EXAMPLE HTML...", 'http://google.com': "<GOOGLE HTML..."}.

  • now you can go through the web addresses, lookup their name in the dictionary and pull out their content when you need to work on it.

87

u/coffeedrinker1994 Jul 03 '20

Goodness, this finally made sense. Thank you for the extremely simple to understand answer.

26

u/Manny__C Jul 03 '20 edited Jul 03 '20

Another important difference is that Tuples, unlike Lists, are "hashable". This means that they can be used as keys in a dictionary. (This, of course, is true only if all the elements of the tuple are hashable as well.)

7

u/atl-knh Jul 03 '20

Because they are immutable!

4

u/Manny__C Jul 03 '20

Now that I think about it, Tuples are immutable, but hashable only if their elements are immutable as well. I'll edit my comment.

3

u/Tsui_Pen Jul 03 '20

I remember learning this but could you elaborate just a little?

Edit: NVM I got it!

3

u/nog642 Jul 04 '20

Also good to note: Elements of a set also have to be hashable, since a set is sort of like a dictionary with only keys.

10

u/ka-splam Jul 03 '20

Great! :)

I was just editing it to add more details, that's probably made it worse now, lol.

11

u/geoelectric Jul 03 '20

I’d mention members of tuples can’t be replaced either (ie tuples are immutable). It’s the other huge difference from lists.

3

u/tworc2 Jul 03 '20

Now that you know that, you may be confortable to learn about nametuples, ordereddicts, ordered sets, and a bunch other of Classes and modules for data :)

6

u/SnowdenIsALegend Jul 03 '20

What are some good real life uses of tuples?

16

u/ka-splam Jul 03 '20

I feel like you don’t “make a tuple to store some data” in a grand and important way like you do with lists and dicts and sets, rather tuples are small and casual and short lived helpers that blend into the background

A common demo in python is to swap the values of two variables like this:

a,b = b,a

Compare to older languages;

temp=a
a=b
b=temp

It works in Python where (a,b) is a tuple and (a,b)=... will try to unpack the collection on the right into enough things to go in the variables. And the thing on the right is (b,a) the two variables effortlessly put in a tuple just by the comma, so they are ready to be unpacked into two things. The tuples are quietly there making it work.

Following that, tuples are a low-effort way to put a few things together just moving data from one line of code to the next, e.g. returning a few things from a function:

def test():
    return 5, 10, 15

first, mid, last = test()

The return value is a tuple; First becomes five, mid becomes ten, last gets fifteen. That’s a convenience you don’t get in more traditional languages where functions can only return one thing, and if you want to return many things you need to make a list, put them in one at a time, return the list, assign it to one variable, then access the items in the list one at a time. Lots more code.

The trick is, Python is only returning one thing - a tuple - but they’re so thoroughly in the language it is so convenient to put things in and take them out without really noticing.

That’s used traditionally when working with dates and times, there’s a 9-tuple like (year, month, day, hour, minute, ...) which the date time functions can work with. (Nowadays they have more complex and capable ways of handling dates too)

It’s used traditionally when reading data about files from the disk:

For backward compatibility, a stat_result instance is also accessible as a tuple of at least 10 integers giving the most important (and portable) members of the stat structure, in the order st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, st_ctime. For compatibility with older Python versions, accessing stat_result as a tuple always returns integers.

Atime is file access time, ctime is creation time, mtime is modification time, size is file size in bytes, and other less common details in there. (Nowadays also there’s higher level things more convenient to work with than this).

The enumerate() function pairs up items in a collection with an index value for their position, walking a list and knowing where you are in the list so you can tell if you’re on the last item, or treat every second item differently, etc.

x,y coordinates in an image or on screen, yes.

(Red, green, blue) values of a pixel in an image.

The fractions module can return tuples of (numerator, denominator)

Low level networking with sockets often talks about (ip_address, port) pairs, so do things like listening webservers and database server connections.

The ipaddress module works with (ip_address, subnet_mask) pairs.

Regex matching on text can return the groups you asked for as tuples.

subprocess returns data from standard output and standard error in tuples.

(Yes I searched the docs for ideas)

(Excuse my phone correcting tuple to triple and tulles all over, I hope I got them all)

(Older, lower level things use tuples more. Newer things tend to have larger objects full of convenience methods; the docs are full of obscure internal uses like exception tuples and the insides of localisation and xml readers and such)

6

u/SnowdenIsALegend Jul 03 '20

Thank you! Highly appreciate the detailed answer, this increased my knowledge by leaps and bounds!

a,b = b,a

Never realized the above was possible. And also didn't know that this and the second example use tuples under the hood. Appreciate the time you put in to give all these examples, thank you!

9

u/ShreddedPump Jul 03 '20

Coordinates maybe?

4

u/el_floppo Jul 03 '20 edited Jul 03 '20

Since tuples are immutable, so they can be good for using with data that you don't want to be changed. If you ever use Python's time module, some of the methods return data as tuples, like struct_time().

Another example is with the SQLite module. If you query a SQLite database, the results are returned in tuples. The fetchone() method returns one result in a tuple and the fetchmany() or fetchall() methods return multiple results as a list of tuples.

Edit: If you do want to change the data, then you just need to unpack it first.

3

u/SnowdenIsALegend Jul 04 '20

Thanks, Appreciate the explanation.

3

u/tworc2 Jul 03 '20

Things that shouldn't be changed at all, either by mistake or malice attempt.

0

u/[deleted] Jul 03 '20

[deleted]

2

u/SnowdenIsALegend Jul 03 '20

Good example, thank you.

4

u/[deleted] Jul 03 '20

Is it also true that lists are 'indexed' meaning they are ordered or named while sets are not?

3

u/ka-splam Jul 03 '20

That sounds right; lists keep things in the order you put them in. I believe sets are built using dictionaries internally, and if you list the keys of a dictionary you can’t expect to get them back in any particular order.

I hesitate because in the PowerShell language there are “ordered dicts” which do keep the order you put things in, and there is talk of using those by default because people keep expecting it to work that way and it doesn’t break anything to make it do so. Wouldn’t surprise me if newer Python versions were doing this and wouldn’t surprise me if they weren’t, but I don’t know.

4

u/ianepperson Jul 03 '20 edited Jul 03 '20

Sets are not built out of dictionaries, but do, sort of, behave like they do.

Sets do not have any order. If you need order, use a list or tuple. Sets are really fast for checking the contents - "if something in collection" is much faster in a set than in a list or tuple.

Python 3 (as of 3.6 I think?) dictionaries are indeed ordered. Older versions of Python do not have ordered dictionaries and many tutorials and developers do not rely on dictionary order - it's relatively new.

4

u/ka-splam Jul 03 '20

“sets are implemented as dictionaries with just keys and no values” - Martin Peters - https://stackoverflow.com/a/15479974

Aha, same answer says “CPython 3.6 introduces a new dict implementation that maintains insertion order, and is faster and more memory efficient to boot.” you’re right.

And “And to be explicit: this change doesn't apply to sets” in the same answer.

2

u/ianepperson Jul 03 '20

That's right. But the order is typically difficult to predict and just happens to have order to it. In any given Python update that order might change - if you write code that depends on it, it could break in the future.

The Python devs work hard to ensure existing code doesn't break, so when they say it's an "implementation detail" they mean that they're feeling free to break that at any time.

3

u/ka-splam Jul 03 '20

it should be stable now, the answer goes on to say

Note that in Python 3.6 this is considered an implementation detail, Python-the-language does not specify that other implementations have to retain order. This changed in Python 3.7, where this detail was elevated to be a language specification; for any implementation to be properly compatible with Python 3.7 or newer it must copy this order-preserving behaviour.

2

u/ianepperson Jul 03 '20

Yes for dictionaries, but I don't believe that's true for sets.

1

u/Silent-Chance1789 Apr 15 '25

I thought it was the other way around that dictionaries could be thought of as a set of tuples (with set logic only applying to keys).... A set is literally a unique collection of items, where a dictionary is a unique collection of keys with associated not necessarily unique values.

That isn't how a dictionary is actually implemented in python but I think it is conceptually better to say that a dictionary is conceptually a derivative of a set as it relies on the concept of unique collection of items which is addressable via a hashmap....

but apparently in reality it is implemented in reverse. But am I conceptually close?

6

u/Natural-Intelligence Jul 03 '20

You missed one essential feature of tuples you cannot overcome with lists: they are immutable. Because of this, you can use tuples as dictionary keys, like you wanted to have a lookup dictionary with keys being the subject and the body of an email: {("BuY mY PRODUCT!", "Hello, this is..."): ... }. And the values could be like who sent it or something. Lists don't work in this case.

3

u/Wooden-Splinter Jul 03 '20

Isnt a list essentially an array in C? Or is there something else im missing?

4

u/_lilell_ print('Hello, World!') Jul 03 '20

Python list being dynamically-sized (you can add or remove elements) is a big difference. I don’t know about C specifically, but I’d compare Python’s list to Java’s ArrayList (without the homogeneous typing constraint, obviously).

3

u/ka-splam Jul 03 '20

sucks teeth ooh I dunno about that. I don’t know enough about C arrays to say, they’re certainly similar in concept and use and performance; Arrays on steroids maybe?

Thing is, a lot of the ideal data structures people talk about get a bit mixed up in actual use; Python lists are so central to the language they have convenience methods to push/pop so you can treat them like a stack or a queue as well. I don’t think C arrays do, but you could use them like that.

Lists are growable, are C arrays? Lists are dynamic typed you can put ints and strings and floats in the same one no problem, can you in C? Lists are objects, C doesn’t have objects, does that matter? Lists are iterable, I don’t think C has iterators or interfaces, does that matter? C arrays I think let you control a lot about memory layout, lists don’t.

Essentially yes maybe?

3

u/primitive_screwhead Jul 03 '20

It's not a linked-list implementation, that's true. It's more like a C++ vector that uses a continuous array of references to the objects, under the hood. Which is why indexing is fast, but also why removing an item from the beginning is slow (a whole new copy of the underlying array must be made).

3

u/value100 Jul 03 '20

This makes it so much easier to understand. Thank you so much.

2

u/ka-splam Jul 03 '20

I’m glad, thanks :)

3

u/Tsui_Pen Jul 03 '20

Just curious, do you (personally) use single quotes and double quotes in different contexts (I know they’re interchangeable in Python)? Like, always single quotes for keys and double for values? Asking because sometimes OCD helps keep things organized lol.

2

u/ka-splam Jul 03 '20

Haha, good question; far from it - I didn't even notice doing that. 😬

I do small amounts of coding jumping around different languages; in PowerShell single-quoted strings are literals and double-quoted strings expand variables inside them. In C# single-quotes are for individual char only and strings must use double quotes. In batch files / command prompt where I have lots of muscle memory, double quotes make a string and single quotes aren't anything. In Dyalog APL it's the other way round, single quotes make a string and double quotes aren't anything. As for style, I think single quotes look cleaner and less cluttered on screen, but double-quotes are what says "string!" to me. And being in the UK, double-quotes (Shift-2) move around with UK/US keyboard layout changes and single-quotes don't.

So no instead of being organised and careful, I use them interchangably depending on whatever takes my eye. At a guess I used single quotes to keep the list of websites looking cleaner, copy-pasted it and used double-quotes from muscle memory - or maybe when I move the cursor with arrow keys Shift-2 is slightly closer than ' ? 🤷‍♂️

3

u/Tsui_Pen Jul 03 '20

Hahaha very thorough answer, thank you

3

u/yuckfoubitch Jul 03 '20

Saved this comment, thanks!

2

u/Sheepoch Jul 03 '20

Thought I’d provide an answer but instead saved yours.

2

u/fun4someone Jul 03 '20

This is really well said.

2

u/grumpyyoshi Jul 03 '20

Great explanation thanks

2

u/nog642 Jul 04 '20

Well index, value in for index, value in enumerate(x): is not a tuple, that's unpacking notation. In this case enumerate yields tuples so it is unpacking a tuple, but you can unpack any iterable, not just tuples.

0

u/[deleted] Aug 02 '20

[deleted]

1

u/ka-splam Aug 03 '20

Great! :)

4

u/AusIV Jul 03 '20

Tuples: I seldom use them outside of certain syntactic use cases, like if I need a function to return multiple values. For example:

def x():
    return 1, 2, 3

a, b, c = x()

makes use of tuples, but I never assigned a tuple anywhere. Tuples can be useful when you need a collection of things that can't change, but in practice I still usually use lists outside of ephemeral syntactic use cases like the one above.

List: Any time I need an ordered collection of things, I use a list.

Dictionary: Map keys to values efficiently. You can't get this from any of the other data structures.

Sets: Sets are most valuable for figuring out of one item belongs to a group of items (membership testing). Suppose I have a 10,000 word essay, and I have a bunch of words that I want to check to see if they appear in the essay. If I do:

for word in wordList:
    if word in essay:
        # do whatever

It has to scan the entire essay for each word to see if it's present. If I do:

essayWordSet = set(essay.split()) # Pretend like capitalization and punctuation aren't a concern
for word in wordList:
    if word in essayWordSet:
        # do whatever

Now instead of having to check every word in wordList against every word in essay, we can look things up in the set much more quickly (because of how the set organizes the data internally). If there are 10,000 words in the essay, this case is 10,000 times faster (ish).

But sets also let you do other things. For example, you could just do:

set(essay.split()) & set(wordList)

To get a list of the words that are in both the essay and the wordList.

The limitation of sets, and why you don't always use them in place of lists are primarily that they aren't ordered and they can't have duplicates. If order matters or your dataset might have multiple copies of an item, you're better off using a list.

3

u/fedeb95 Jul 03 '20

Aside from tuples, the other use cases become clear when you examine their time and space performance and characteristics. For instance, stets only store each different element once. So let's say you want to have a collection without duplicates, you use a set. A dictionary (or hash map) has a key-value structure, and is very fast in retrieving an element from it. So if you need that to be fast, go for a dictionary. Other probably explained use cases more in depth, but I suggest you to learn some more data structure theory to really grasp why and when to use what

3

u/[deleted] Jul 03 '20

Tuples: immutable, have order, not sortable.

Tuples are for where position has meaning. And by meaning, i'm not talking about sorting.

In a class room, the first item is teacher, second item is number of students, third item is class subject, fourth item is grade level.

List: mutable, have order, sortable.

Lists are for when being able to sort is important. For example, the list would be names of students in order of academic performance. This will change, and require resorting.

Lists can be added to and removed from, unlike tuples. But that's not their "key feature." The key feature is it that they can be sorted and resorted, that is position is likely to change.

Sets: mutable, no order, no sorting.

Sets have no order, but that's not the point. The point is that you can treat them arithmetically. `active_users = all_users - expired_users`.

3

u/bbt133t Jul 03 '20

You wanted simple so 4 lines is what you get, why and when to use:

  • Lists - mutable and allow duplicate elements

  • Tuples - same as Lists but immutable

  • Sets - same as lists but do not allow duplicate elements

  • Dictionaries - a combination of keys behaving like a set and values behaving like a list

6

u/Cwlrs Jul 03 '20 edited Jul 03 '20

Tuples: Immutable (can't be changed once assigned information). Use cases: For some libraries and functions, the input type of some arguments must be a tuple. Even thinking now, I can't think of the reason why they needed it to be a tuple as opposed to a list, but this is my personal day to day reason for using a tuple. I specifically remember having to use the syntax (var,) with a randomly placed comma in a tuple of length 1 to make it a tuple type.

Lists: Mutable (can have items added, removed, order changed, etc) and are the most versatile, and easy to use data type. These are useful for storing many unique and non unique values, where the order of them may or may not be important to you later, and there is no other associated data with a single set of values. Or if there is, consider using a list of lists. Lists are my main weapon of choice for many scenarios. Pros: most versatile, and can be used in almost all situations. Cons: will likely be slower than the following data structures for certain tasks regarding unique values, and for finding data stored in a data structure.

Dictionaries: Mutable, hash tables. These are excellent for storing data (value) against a certain key. e.g. counting the number of wins and appearances of a certain thing, like highest dice roll, then you can do dict_wins['5']/dict_apps['5'] where you have run many simulations of the dice roll, and constantly added to the dict[value] by using +=1 for example. Dicts are also very fast at accessing a specific key due to the nature of the hash table. e.g. if you search for dict_wins['5'], it's very fast, whereas searching a huge list for the first '5' value or the count of '5' is much longer. I mainly use dictionaries to count rolling totals or values against a certain type of key value, some other variable maybe like a dice roll, or a string description of something. They are useful for string based data too, and look and feel like .json file, commonly used for configuation. So you can read a .json file and parse the data into a dictionary. Useful for making your own config files too, especially compared to a list, where you can do dict_config['login_pw'] - and then add new information to the dict, and you won't mess up the structure, as opposed to if you have list_config[2] and added a new value at the start of the list, you will now be referring to the wrong data. The dictionary structure is better than the list here.

Sets: Similar to dictionaries, but are mainly used in coding games to have the fastest code. They are mainly used to test for uniqueness of values in a dataset. Using set_values.add(value) adds a value to a set, and if the value already exists in the set, nothing is added. Only new unique values are added. You can find the length of the set to find the number of unique values that have appeared in your project, you can iterate over all values in the set like, for i in set: print(i). Which you can't do for a dictionary. However it will be an unordered for loop through the set, due to the nature of the hash function. You cannot order a set. Main benefits are the insane speed for testing uniqueness of a dataset, drawbacks are it is very inflexible and can't achieve much else from my experience.

3

u/ianepperson Jul 03 '20

You absolutely can iterate over a dictionary. (for i in dictionary definitely works. ) In fact, you have three options for doing that.

For i in dictionary - walks through just the keys. For i in dictionary.keys() does the same, but a bit more explicit.

For i in dictionary.values() walks you through the values stored in the dictionary. I find this less useful, but it's good to know about.

The most useful is "dictionary.items()" which gives you both in a tuple. For key, value in dictionary.items() works well and is really useful.

2

u/Cwlrs Jul 03 '20

Oh cool, I literally didn't know that! Thought that made my programs crash back in the day. I thought I would have tried it by now.

2

u/COMPROTKS-8881 Jul 03 '20

HI IAM A BEGINNER,

But I know-how list and tuples vary it is quite simple

*LIST => A list contains millions of values like the alphabet , numeric (0-9) and each element is enclosed by square brackets.For instance [1,'animal','car','bike'].These are mutable(changeable) we can change,add,remove.

*TUPLE=> Tuple is immutable. Once we created a tuple we can't change it

7

u/[deleted] Jul 03 '20 edited Jul 03 '20

[removed] — view removed comment

1

u/coffeedrinker1994 Jul 03 '20

Very grateful for your answer, this sort of thing always makes sense to me when somebody explains using really simple examples, such as your fruit example.

1

u/blazingshadow1 Jul 03 '20

Also I would like to add just one thing. One main difference when it comes to sets it that they are extremely efficient at checking membership of a value. For example if you want to know if 3 is there in a list of 100 items and if you wanna know if 3 is there in a set of 100 items. It will take longer time in list. In set the check for membership takes the same time regardless of it's size.

2

u/jwink3101 Jul 03 '20

I assume you meant to change the second "100" to something like "1000".

To OP: This is one of the many great uses for sets!

Also, pro-tip, if for some reason you are still support python 2:

First of all, stop supporting python 2! Second of all, if you're checking for membership in a dictionay do:

mykey in mydict

and not

mykey in mydict.keys()

In python2, the latter first makes a list and the checks the membership in the list. The former uses set-like opperations.

Quick testing is that the former is also faster in python 3.7 but not by much.

6

u/gardyna Jul 03 '20

Great answer, but you did miss one of the most important aspect of sets (and IMO the most useful one). sets do not contain duplicates. one way to check if a list contains duplicates is converting it to a set and seeing if the length is the same

myList = [1, 2, 3, 4, 4, 5,...]
containsDuplicates = (len(myList) == len(set(myList)))