Skip to content

Latest commit

 

History

History
60 lines (49 loc) · 1.83 KB

File metadata and controls

60 lines (49 loc) · 1.83 KB

Understanding Lists and List Data Structure

What is a List?

A list is a fundamental data structure in programming that allows you to store a collection of items. Lists are ordered and can contain elements of various data types, such as numbers, strings, and objects.

Creating Lists

You can create a list in various programming languages. In Python, for example, you create a list using square brackets:

my_list = [1, 2, 3, 'apple', 'banana']

List Indexing

List elements are indexed, starting from 0 for the first element. You can access elements by their index.

first_element = my_list[0]  # Access the first element (1)

List Length

You can find the length of a list using the len() function.

list_length = len(my_list)  # Length of the list (5)

List Manipulation and Common List Operations

Appending to a List

You can add elements to the end of a list using the append() method.

my_list.append(4)  # Adds 4 to the end of the list

Removing from a List

You can remove elements by their value using the remove() method.

my_list.remove('apple')  # Removes 'apple' from the list

Slicing a List

Slicing allows you to create a new list from a subset of the original list.

subset = my_list[1:4]  # Creates a new list with elements at index 1, 2, and 3

Concatenating Lists

You can combine two or more lists to create a new list.

new_list = my_list + [5, 6]  # Concatenates my_list with [5, 6]

Sorting a List

You can sort a list in ascending or descending order using the sort() method.

my_list.sort()  # Sorts the list in ascending order

Checking for an Element

You can check if an element exists in a list using the in keyword.

is_present = 'banana' in my_list  # Checks if 'banana' is in the list (True)