Python Data Structure Cheat Sheet – List

0
65

Python Data Structures

Data Structure means how to organize and store data efficiently So it’s easier to access and modify them in ease.

Lists in Python

A list is a heterogeneous container for items. They are used to store multiple items in a variable. This is a built-in data type in Python.

Properties of a Python List

This is important to understand the properties of a collection type because it provides the developer with the understanding of what can be done using this type.

The list items are Ordered, Changeable and allows having duplicate values.

Python List Cheat Sheet

Examples covered in this cheat sheet –

  • Declaring a Python list
  • Assigning a list to a variable
  • Appending an item to the list using index
  • Accessing a single item from a list using index
  • Slicing a list
  • Reassigning an item to the list
  • Nested lists
  • Accessing a nested item in a list

Examples of List Properties and Methods

Below are some examples which you can copy paste in Jupyter Notebook or any other Python IDE and see the results. These are the same codes which are shown above.

[1,2,3] # Declaring a Python list
['hi',1,[1,2]] # Can contain numbers, float and characters
my_list = ['a','b','c']  # Assigning a list to a variable
my_list.append('d') # Appending an item to the list using index
my_list[0] # Accessing a single item from a list using index
my_list[1:]  # Slicing a list
my_list[:1] # Slicing a list
my_list[0] = 'NEW'  # Reassigning an item
nest = [1,2,3,[4,5,['target']]]  # Nested list
nest[3]  # Accessing a nested item (step 1)
nest[3][2]  # Accessing a nested item (step 2)
nest[3][2][0]  # Accessing a nested item (step 3)

LEAVE A REPLY

Please enter your comment!
Please enter your name here