Are Python lists and dictionaries immutable?
Data Structures- Lists, Tuples, Dictionaries, and Sets in PythonMar 13, 2019·5 min read I had quite a bit of a hard time figuring out how to do one of my assignments because I was still getting the hang of these data structures. But after going through this lesson, it helped me get a better understanding of how I can use them. Below is a summary of my learnings. A List, Set, Tuple, and Dictionary are one of the most common and basic data structures in Python. Data structures are containers that organize and group data types together in different ways. Mutability and Order Mutability is about whether or not we can change an object once it has been created. If an object can be changed then it is called mutable. However, if an object cannot be changed with creating a completely new object, then the object is considered immutable. Order is about whether the position(index) of an element in the object can be used to access the element. Lists Lists can contain any mix and match of the data types you have seen so far. You can create a list with square brackets like below. Lists are both mutable and ordered. list_of_random_things = [1, 3.4, 'a string', True]Tuples A tuple is a data type for immutable ordered sequences of elements. They are often used to store related pieces of information. Tuples are similar to lists in that they store an ordered collection of objects which can be accessed by their indices. Unlike lists, however, tuples are immutable you cant add and remove items from tuples, or sort them in place. location_a = (13.4125, 103.866667)location_b = 13.4125, 103.866667 The parentheses are optional when defining tuples, and programmers frequently omit them if parentheses dont clarify the code. location_a and location_b are the same. Sets A set is a data type for mutable unordered collections of unique elements. One application of a set is to quickly remove duplicates from a list. numbers = [1, 2, 6, 3, 1, 1, 6]unique_nums = set(numbers) print(unique_nums)#Output would be: {1, 2, 3, 6} Dictionaries A dictionary is a mutable data type that stores mappings of unique keys to values. Heres a dictionary that stores elements and their atomic numbers. elements = {"hydrogen": 1, "helium": 2, "carbon": 6}Dictionaries can have keys of any immutable type, like integers or tuples, not just strings. Its not even necessary for every key to have the same type! We can look up values or insert new values in the dictionary using square brackets that enclose the key.
How I used these lessons to solve a challenge:Write a function that takes a string and returns a tuple containing a new string made of all and only the vowels from the original string and the number of duplicates in the original string. Only the first instance of the vowel is considered. For example: countVowels(dahdah) # will return (a, 3) countVowels(drink water) # will return (iae, 1) Description of the code above:
If you found this helpful, feel free to share or comment. |