DEV Community

Cover image for PYTHON DATA TYPE[LIST]
Christopher Ambala
Christopher Ambala

Posted on

PYTHON DATA TYPE[LIST]

A list is an ordered,mutable and heterogeneous collection of items

Creating a list

We use the syntax my_list

my_list = ['apple','banana','cherry']
Enter fullscreen mode Exit fullscreen mode

Accessing List Items

The index number is used in reference, starting from index 0 and second item being 1.

first_item = my_list[0]
print(first_item)  #Outputs: apple
Enter fullscreen mode Exit fullscreen mode

Modifying List Items

This is done by referring to their index.

my_list[1] = 'blueberry'
print(my_list) #Outputs:['apple','blueberry','cherry']
Enter fullscreen mode Exit fullscreen mode

List Comprehension

It's a syntactic construct that enables list to be created from other lists.

new_list = [expression for item in iterable if condition]

Enter fullscreen mode Exit fullscreen mode

expression is an operation applied to each item in the iterable that satisfies the condition.
item is a variable used to represent members of the iterable.
iterable is a sequence, collection, or an iterator object to be traversed.
condition is an optional filter that only includes item in the new_list if the condition is True.

squares = [x**2 for x in range(10) if x % 2 == 0]

Enter fullscreen mode Exit fullscreen mode

This will result in squares being a list of the squares of all even numbers from 0 to 9: [0, 4, 16, 36, 64].

List comprehensions are a powerful feature of Python and can make your code more readable and efficient.

List Operations

  • append(): Adds an element at the end of the list.
  • extend(): Add the elements of a list (or any iterable), to the end of the current list.
  • insert(): Adds an element at the specified position.
  • remove(): Removes the item with the specified value.
  • pop(): Removes the element at the specified position.
  • index(): Returns the index of the first element with the specified value.
  • count(): Returns the number of times a value appears in the list.
  • sort(): Sorts the list.
  • reverse(): Reverses the order of the list.
# Create a list
fruits = ["apple", "banana", "cherry"]

# Add an element to the end of the list
fruits.append("orange")

# Add multiple elements to the end of the list
fruits.extend(["kiwi", "mango"])

# Add an element at a specific position
fruits.insert(1, "pineapple")

# Remove an element from the list
fruits.remove("banana")

# Remove the last element in the list
last_fruit = fruits.pop()

# Get the index of the first occurrence of an element
index_of_cherry = fruits.index("cherry")

# Count the number of times an element appears in the list
num_apples = fruits.count("apple")

# Sort the list
fruits.sort()

# Reverse the list
fruits.reverse()

Enter fullscreen mode Exit fullscreen mode

List Concatenation

Can Be done in three ways;
Using the + Operator: This is the most straightforward method. The + operator can be used to add together two lists:

list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list1 + list2  # combined is now [1, 2, 3, 4, 5, 6]

Enter fullscreen mode Exit fullscreen mode

Using the extend() Method: The extend() method adds elements from another list (or any iterable) to the end of the current list:

list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)  # list1 is now [1, 2, 3, 4, 5, 6]

Enter fullscreen mode Exit fullscreen mode

Using List Comprehension: This is a more advanced method that involves creating a new list based on existing lists. It’s a concise way to create lists based on existing lists (or other iterables)

list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = [item for sublist in [list1, list2] for item in sublist]  # combined is now [1, 2, 3, 4, 5, 6]

Enter fullscreen mode Exit fullscreen mode

Top comments (0)