Everything you need to know about Python Lists- Part 1/2 (2022)

Lists:- Basic Properties, Slicing, Indexing, in-built functions

Parichay Pothepalli
2 min readApr 3, 2022

Lists are used to store multiple items in a single variable. It is always represented in square brackets and the inner elements are seperated by commas. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

Snippet 1

Snippet 1: The list allows for users to input multiple datatypes such as lists, tuples, floats, integers and character values.

Snippet 2

Snippet 2: A list object can be a placeholder for a function with it’s return object being supported by the list, in this case the int value of 81.

Accessing elements of a list:-

Lists can be accesed with indexes and the convention in python suggest starting at index=0 and going uptil index =len(list)-1. There also exists negative indexes in case the length of the list is unknown.( Figure 1)

Figure 1: Forward and Negative Indexes
Snippet 3: Forward and Negative indexing

List Functions:-

  1. list():- This function allows us to create lists from different data structures such as strings, tuples and any other iterable object in specific.
Snippet 4: List() function implementation on various Data Structures

2. len():- It tells us the number of elements present in the list ( implementation in Snippet 5)

3. max()/min():- It is used to find the largest/smallest value in an iterable.( implementation in Snippet 5)

4. sum():- It is used to provide the sum of all the elements in the list.( implementation in Snippet 5)

Note:- Sum() cannot be used for non-numeric values. A combination of numeric and non-numeric values as well will throw an error.

Snippet 5: min(), max() and sum()

5. in() and not in():- These are called as membership operators. As the name suggests these help us tell whether a specific element is present in the list/not.

Snippet 6: in() and not in()

List Slicing:-

Although we did cover a fair bit on indexes, we shall deep dive into indexing with more customizability.

Syntax:- List[start_val:end_val:step_val]

The slice begins with the element whose index is given by start_val (default 0), until 1 less than the end_val , in increments of the step_val (default is 1).

Snippet 7: List slicing

Congratulation’s guys on making it so far. If you haven’t coded out each and every line, I’d suggest you to do so as programming requires hand’s on practice. The next module in Lists covers methods, aliases/memory allocation and list comprehensions.

Do like and follow if you are able to comfortably follow along the steps. If not please leave a comment and i’d be more than happy to help you out and address my content.

--

--

Parichay Pothepalli

Data Scientist who believes that AI/ Deep Learning is here to change the way we view the world. Join me in this journey of growth, sharing experiences.