Beginner to python: Lists, Tuples, Dictionaries, Sets - Stack Overflow
When should I use a list, dictionary, tuple, or set in Python?
Why doesn't dict.items() and dict.keys() return a list?
What is the benefit of these objects?
A list has to load all its data into memory before doing anything. These objects just reference the underlying dict, not duplicating anything. This means that they are less memory-hungry and that, if you are apt to exit the loop quickly, you didn't pay to copy everything beforehand.
To me they just seem to introduce unnecessary complexity to my code.
It is one more thing to keep in mind, and I share your skepticism about its value. Ultimately, few will share it.
FWIW, numpy.array does more guessing than your average nice function, and this leads to a lot of stuff like this. I'm not sure that there is a nice API it could provide that didn't have such hiccups sometimes, though.
Real-world example of using lists/tuples/dictionaries?
Explain the process and benefit of using list methods like 'insert', 'remove', and 'sort' in Python list manipulation.
How can nested dictionaries in Python be used to represent complex data structures, and what is one example of accessing nested values?
Why is the 'pop()' method in Python significant for managing list data, and how can it be used differently?
List and tuple are similar to dynamic arrays where in list are mutable but tuples are not. You could checkout the documentation for list and tuple https://docs.python.org/3.8/library/stdtypes.html#sequence-types-list-tuple-range
A set is an unordered collection of objects. Link to the docs: https://docs.python.org/3.8/library/stdtypes.html#set-types-set-frozenset
A dict maps hashable values to arbitrary objects. Mappings are mutable objects. Link to the docs: https://docs.python.org/3.8/library/stdtypes.html#mapping-types-dict
List : Used to store the items. Duplicates can also be stored. Order of storing will be maintained as is. It is mutable which means the same object can be altered after creation. Example :
list1 = ['one',2,3.4,'hello'] print(list1) print(list1[3]) list1[1]=2 #allowed print(list1)Answer:
'one', 2, 3.4, 'hello'] hello [2, 2, 3.4, 'hello']Set : Just like list. Only difference is it cannot store the duplicate values. Also there will be no order in which values are stored.
Example:
>>>#set of integers
>>>s = {1, 2, 3}
>>>print(s)
{1, 2, 3}
>>>#print type of s
>>>print(type(s))
<class 'set'>
>>> x = set(['foo', 'bar', 'baz', 'foo', 'qux'])
>>> x
{'qux', 'foo', 'bar', 'baz'}
- Dictionary : Dictionary is basically a storage type in which you can store a value corresponding to a key just like a real life dictionary.
Example:
>>>dictval = {"Nishant":1,"Akash":2,"Ravi":3,"Hari":4}
>>>print(dictval)
{'Nishant': 1, 'Akash': 2, 'Ravi': 3, 'Hari': 4}
>>>print(dictval["Akash"])
2
- Tuple: A tuple is a sequence of values much like a list. The values stored in a tuple can be any type, and they are indexed by integers. The important difference is that tuples are immutable.That we can’t change the elements of tuple once it is assigned whereas in the list, elements can be changed.
Example:
>>>#tuple with mixed datatypes
>>>t = (1, 'raju', 28, 'abc')
>>>print(t)
>>>print(t[1])
(1, 'raju', 28, 'abc')
raju
>>>t = (1, 'raju', 28, 'abc')
>>>print(t)
>>>print(t[3])
>>>t[3] = 'def'
(1, 'raju', 28, 'abc')
abc
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-27-254e32ad7a3c> in <module>()
2 print(t)
3 print(t[3])
----> 4 t[3] = 'def'
TypeError: 'tuple' object does not support item assignment
Hey everyone,
I’m trying to get a deeper understanding of Python’s built-in data structures—specifically lists, dictionaries, tuples, and sets. I understand their basic properties:
Lists (list) → Ordered, mutable, allows duplicates
Tuples (tuple) → Ordered, immutable, allows duplicates
Dictionaries (dict) → Key-value pairs, unordered (but ordered since Python 3.7), keys must be unique
Sets (set) → Unordered, mutable, unique elements only
But I'm struggling to figure out when to use each of them in real-world scenarios. I know some general guidelines, like:
Use lists when order matters and you need to modify elements.
Use tuples when you need an immutable sequence (e.g., function return values).
Use sets when you need fast lookups and uniqueness.
Use dictionaries when mapping keys to values efficiently.
However, I’d love to hear from experienced Python developers:
Are there any specific patterns or real-world examples where one of these structures is clearly the best choice?
Have you ever misused one and later realized another structure would have been better?
Are there any performance considerations that I should be aware of when choosing between them?
Looking forward to your insights! Thanks in advance.