1.

Lists in Python

Answer»

Lists are used to store multiple items in a single variable. Their usage and some functions are shown below with examples:

example = ["Sunday", "Monday", "Tuesday", "Wednesday"];
print(example)
Output:
['Sunday', 'Monday', 'Tuesday', 'Wednesday']

  • Accessing elements in a List:
    Accessing elements in a list basically means getting the value of an element at some arbitrary index in the list.
    Indexes are assigned on 0 based basis in python. We can also access elements in python with negative indexes. Negative indexes represent elements, counted from the back (end) of the list.
example = ["Sunday", "Monday", "Tuesday", "Wednesday"];
# Positive Indexing
print(example[0], example[1])
# Negative Indexing
print(example[-1])
Output:
Sunday Monday
Wednesday

  • Slicing a List:
    Slicing is the process of accessing a part or subset of a given list. The slicing is explained in the image below:
example = ["Sunday", "Monday", "Tuesday", "Wednesday"];
# Positive Slicing
print(example[0:2])
# Negative Slicing
print(example[-3:-1])
Output:
['Sunday', 'Monday']
['Monday', 'Tuesday']

  • Changing Values in a List:
    We can change values at some particular index in a list by accessing the element with [] and then setting it to some other value.
example = ["Sunday", "Monday", "Tuesday", "Wednesday"];
print(example)
example[0] = "Saturday"
print(example)
Output:
['Sunday', 'Monday', 'Tuesday', 'Wednesday']
['Saturday', 'Monday', 'Tuesday', 'Wednesday']
  • List Concatenation and Replication:

When we merge the contents of 2 lists into one list, it is called list concatenation.

example = ["Sunday", "Monday", "Tuesday", "Wednesday"];
example1 = ["Weekdays", "Weekends"]
# Concatenation
example = example + example1
print(example)
Output:
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Weekdays', 'Weekends']

Copying the contents of a list, some finite number of times into the same or some list is called list replication.

example = ["Sunday", "Monday", "Tuesday", "Wednesday"];
example1 = ["Weekdays", "Weekends"]
# Replication
example1 = example1 * 3
print(example1)
Output:
['Weekdays', 'Weekends', 'Weekdays', 'Weekends', 'Weekdays', 'Weekends']

  • Delete values from Lists:
    We can delete a particular element from a list by using the del keyword.
example = ["Sunday", "Monday", "Tuesday", "Wednesday"];
print(example)
del example[2]
print(example)
Output:
['Sunday', 'Monday', 'Tuesday', 'Wednesday']
['Sunday', 'Monday', 'Wednesday']

  • Looping through Lists:
    The below example shows how we can iterate over all the elements present in a list.
example = ["Sunday", "Monday", "Tuesday", "Wednesday"];
for ex in example:
print(ex)

Output:
Sunday
Monday
Tuesday
Wednesday

in and not in keywords:
With the in keyword, we can check if some particular element is present in the given python variable.
Similar to the not in keyword, we can check if some particular element is not present in the given python variable.

example = ["Sunday", "Monday", "Tuesday", "Wednesday"];
print("Sunday" in example)
print("Hello" not in example)
Output:
True
True
  • Adding Values in Lists:

insert(): This function inserts an element into a particular index of a list.

example = ["Sunday", "Monday", "Tuesday", "Wednesday"];
print(example)
example.insert(1, 'Days')
print(example)
Output:
['Sunday', 'Monday', 'Tuesday', 'Wednesday']
['Sunday', 'Days', 'Monday', 'Tuesday', 'Wednesday']

append(): This function appends an element at the back of a list.

example = ["Sunday", "Monday", "Tuesday", "Wednesday"];
print(example)
example.append('Days')
print(example)
Output:
['Sunday', 'Monday', 'Tuesday', 'Wednesday']
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Days']

  • Sorting a List:
    Sorting a list means arranging the elements of the list in some particular order. We sort a list by using the sort() function.
# Sorts in lexicographical order
example = ["Sunday", "Monday", "Tuesday", "Wednesday"];
print(example)
# Sort in ascending order
example.sort()
print(example)
# Sort in descending order
example.sort(reverse = True)
print(example)

example = [1, 5, 3, 7, 2]
# Sort in ascending order
example.sort()
print(example)
# Sort in descending order
example.sort(reverse = True)
print(example)
Output:
['Sunday', 'Monday', 'Tuesday', 'Wednesday']
['Monday', 'Sunday', 'Tuesday', 'Wednesday']
['Wednesday', 'Tuesday', 'Sunday', 'Monday']
[1, 2, 3, 5, 7]
[7, 5, 3, 2, 1]


Discussion

No Comment Found