Some Basic Python List Programs-3

Python program to remove duplicates from a list

  • List comprehension is a compact method that defines a list and its contents at the same time. The basic syntax of list comprehension consists of a square bracket with an expression that is executed for each element along with for loop to iterate over each element. Read more: List Comprehension in Python – Explained with Examples
  • The append() built-in list method adds the element of the iterable passed as an argument to the end of the list.
  • The built-in enumerate() function assigns an index to each element in an iterable and each time returns a tuple of the form (index, element). Read more: What is indexing, iterating and enumerating in python?
  • Slicing extracts a subset of elements from a sequence. It mainly uses three parameters strat, stop, and step enclosed in square brackets []. Read more: Slicing in python
  • The set() built-in method creates a set of the iterable passed. A set is an unordered collection of immutable and unique elements in python.
  • The list() built-in method takes an iterable and creates a new list of its elements.

Python program to check if one list is a subset of another list

  • The all() built-in method returns True If all the elements in the iterable passed are true else it returns False.
  • Python treats everything as an object and objects are always true in python. However, null values, None, 0, empty data types are observed as false in python.
  • The format() built-in string method returns a formatted representation of the string controlled by the format specifier. The prefix f before a string tells Python that the string is a format string. The replacement fields are expressions enclosed in curly brackets in a format string. They are evaluated at run time and then formatted.
  • The issubset() built-in method returns True if all the elements of a set are present in the iterable passed else it returns False.
  • A set in python represents a mathematical notion of a set and performs set operations like union, intersection, difference, etc. The ‘&’ operator executes the intersection between sets. Read more: Set Data Type in Python

Python program to check if all elements in a List are same

  • The count() built-in list method returns the number of times a specified element appears in the list.

Python program to insert a given string at the beginning of all elements in a list

  • The % operator is a format place holder for C-style formatting in a string. The “s” after the “%” signifies that the variable inserting in that position is of the string type.
  • The built-in map() function applies a given function to each element of an iterable and returns a list of the results.

Python program to pair consecutive elements in a list

  • The zip() built-in method returns an iterator of tuples, grouping the similar index of multiple sequences so that they can be used as a single entity.

Related Posts-

Add Comment

%d bloggers like this: