Python_Basic Concepts

List, Dictionary, Set and Tuple

Sanjay.M
AIKISS

--

Photo by Rene Böhmer on Unsplash
  • Dictionary — A dictionary is a mutable unordered collection that Python indexes with name and value pairs.
  • List — A list is a mutable ordered collection that allows duplicate elements.
  • Set — A set is a mutable unordered collection with no duplicate elements.
  • Tuple — A tuple is an immutable ordered collection that allows duplicate elements.

Lists and Tuples

Lists and tuples are very similar in Python and are often confused. The significant difference is that a list is mutable, but a tuple isn’t. So, we include a list when we want to contain similar items, and include a tuple when we know what information goes into it ahead of time.

Both lists and tuples hold an ordered collection of items.

The primary difference is that you will see syntactically is that a list is enclosed by square braces [] and a Tuple is enclosed by parenthesis (). The following code defines both list and tuple.

List is mutable, which means the program can change it. A tuple is immutable, which means the program cannot change it. The following code demonstrates that the program can change a list. This code also illustrates that Python indexes lists starting at element 0. Accessing element one modifies the second element in the collection. One advantage of tuples over lists is that tuples are generally slightly faster to iterate over than lists. One advantage of tuples over lists is that tuples are generally slightly faster to iterate over lists.

Like many languages, Python has a for statement. This statement allows you to loop over every element in a collection, such as a list or a tuple

The enumerate function is useful for enumerating over a collection and having access to the index of the element that we are currently on.

A list can have multiple objects added to it, such as strings. Duplicate values are allowed. Tuples do not allow the program to add additional objects after definition.

Ordered collections, such as lists and tuples, allow you to access an element by its index number, such as is done in the following code. Unordered collections, such as dictionaries and sets, do not allow the program to access them in this way.

A list can have multiple objects added to it, such as strings. Duplicate values are allowed. Tuples do not allow the program to add additional objects after definition. For the insert function, an index, the programmer must specify an index. These operations are not allowed for tuples because they would result in a change.

Sets

A Python set holds an unordered collection of objects, but sets do allow duplicates. If a program adds a duplicate item to a set, only one copy of each item remains in the collection. Adding a duplicate item to a set does not result in an error. Any of the following techniques will define a set.

A list is always enclosed in square braces [], a tuple in parenthesis (), and now we see that the programmer encloses a set in curly braces. Programs can add items to a set as they run. Programs can dynamically add items to a set with the add function. It is important to note that the append function adds items to lists and tuples, whereas the add function adds items to a set.

Maps/Dictionaries

Many programming languages include the concept of a map, dictionary, or hash table. These are all very related concepts. Python provides a dictionary, that is essentially a collection of name-value pairs. Programs define dictionaries using curly-braces, as seen here.

You can also access the individual keys and values of a dictionary.

Dictionaries and lists can be combined. This syntax is closely related to JSON. Dictionaries and lists together are a good way to build very complex data structures. While Python allows quotes (“) and apostrophe (‘) for strings, JSON only allows double-quotes (“). We will cover JSON in much greater detail later in this module.

The following code shows a hybrid usage of dictionaries and lists.

The variable customers is a list that holds three dictionaries that represent customers. You can think of these dictionaries as records in a table. The fields in these individual records are the keys of the dictionary. Here the keys name and pets are fields. However, the field pets holds a list of pet names. There is no limit to how deep you might choose to nest lists and maps. It is also possible to nest a map inside of a map or a list inside of another list.

More Advanced Lists

Several advanced features are available for lists that this section introduces. One such function is zip. Two lists can be combined into a single list by the zip command. The following code demonstrates the zip command.

The usual method for using the zip command is inside of a for-loop. The following code shows how a for-loop can assign a variable to each collection that the program is iterating.

Sometimes you may wish to know the current numeric index when a for-loop is iterating through an ordered collection. Use the enumerate command to track the index location for a collection element. Because the enumerate command deals with numeric indexes of the collection, the zip command will assign arbitrary indexes to elements from unordered collections.

The comprehension command can dynamically build up a list. The comprehension below counts from 0 to 9 and adds each value (multiplied by 10) to a list.

A dictionary can also be a comprehension. The general format for this is: dict_variable = {key:value for (key,value) in dictonary.items()}

This can be used to easily find the index of a column by name.

--

--

Sanjay.M
AIKISS
Editor for

Certified Data Science Associate, Machine Learning and AI Practitioner Github:-https://github.com/Msanjayds, Linked in: https://www.linkedin.com/in/sanjaymds/