DEV Community

Cover image for Day 5: Python Lists Demystified: Tips, Tricks, and Best Practices
Praneeth
Praneeth

Posted on

Day 5: Python Lists Demystified: Tips, Tricks, and Best Practices

A list is a built-in data structure that represents an ordered, mutable and indexable collection of items. It is similar to the arrays in c. but in the case of lists, they can be dynamically allocated.
In python, the list is represented with with values separated by ",".

x = [1, 2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode
  • Dynamically Allocated: a data structure can grow (or shrink) in memory as needed.
  • Mutable: An object can be modified after it has been created.
  • Indexability: the ability of a data structure to allow access to its elements using an index.

Image description

List Assignment:

There are two main scenarios to consider: direct assignment and copying.

1. Direct Assignment

When you directly assign one list to another variable, both variables point to the same list object in memory. Changes made via one variable will affect the other.

a = [1 , 2 , 3 , 4]
b = a
b[3] = 10
print(b) #[1,2,3,10]
print(a) #[1,2,3,10]
Enter fullscreen mode Exit fullscreen mode

2.Copying a List

To create an independent copy of a list, you need to explicitly copy it using methods like:

1. Using List():

list2 = list(list1)
Enter fullscreen mode Exit fullscreen mode

2. Using copy():

List2 = List1.copy()
Enter fullscreen mode Exit fullscreen mode

List Slicing:

List slicing is a technique used to extract a subset of elements from a list. It allows you to create a new list that includes a specific range of elements from an existing list.

syntax

List2 = List1[start : stop : step] 
Enter fullscreen mode Exit fullscreen mode

Example:

c = [10, 2, 3, 4]
d = c[0 : 3 : 1]
print(d) ##[10, 2, 3]
Enter fullscreen mode Exit fullscreen mode

Nested Lists:

In python, a List can consist of another lists as elements

list1 = [[1,2,3,4],[5,6,7,8]]
Enter fullscreen mode Exit fullscreen mode

Built-In Functions:

1. append()

Adds an element to the end of the list.

List1 = [1, 2, 3]
List1.append(4)
print(List1)  #[1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

2. pop()

Removes and returns an element at a specific index (defaults to the last element).

my_list = [1, 2, 3]
removed_element = my_list.pop()
print(removed_element)  # 3
print(my_list)  #[1, 2]
Enter fullscreen mode Exit fullscreen mode

3. remove()

Removes the first occurrence of a specific element.

my_list = [1, 2, 3, 2]
my_list.remove(2)
print(my_list)  # [1, 3, 2]
Enter fullscreen mode Exit fullscreen mode

4. clear()

Removes all elements from the list, making it empty.

my_list = [1, 2, 3]
my_list.clear()
print(my_list)  # Output: []
Enter fullscreen mode Exit fullscreen mode

5. index()

Returns the index of the first occurrence of a specified element.

my_list = [1, 2, 3]
print(my_list.index(2))  # Output: 1
Enter fullscreen mode Exit fullscreen mode

6. count()

Returns the number of occurrences of a specified element in the list.

my_list = [1, 2, 2, 3, 2]
print(my_list.count(2))  # Output: 3
Enter fullscreen mode Exit fullscreen mode

7. sort()

Sorts the elements of the list in ascending order (modifies the list in place).

my_list = [3, 1, 2]
my_list.sort()
print(my_list)  # Output: [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

8. reverse()

Reverses the order of the elements in the list (modifies the list in place).

my_list = [1, 2, 3]
my_list.reverse()
print(my_list)  # Output: [3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

9. min() and max()

min() returns the smallest element in the list.
max() returns the largest element in the list.

my_list = [1, 2, 3, 4]
print(min(my_list))  # Output: 1
print(max(my_list))  # Output: 4
Enter fullscreen mode Exit fullscreen mode

10. sum()

Returns the sum of all elements in the list (useful for lists of numbers).

my_list = [1, 2, 3, 4]
print(sum(my_list))  # Output: 10
Enter fullscreen mode Exit fullscreen mode

Top comments (0)