• 04 October

    Get Every Other Element in List in Python

    A list is one of the more versatile data types among the others provided in Python. As we know lists can store multiple items in one single variable, there is often a need to get only a handful of items from a given list. Get Every Other Element in List in Python This tutorial focuses […]

  • Convert String List to Integer List in Python
    27 April

    Convert String List to Integer List in Python

    Using map() Method Use the map() method with list() method to convert string list to integer list in Python. [crayon-65f8f8db48198312553977/] [crayon-65f8f8db4819d901370760/] First, we defined a variable named string_list and set its value to a list having five string values. Next, we used the map() method, which applied the int() method to every element in string_list […]

  • Convert List to Integer in Python
    28 February

    Convert List to Integer in Python

    Using for Loop To convert the entire list into one integer in Python: Create a list having integer-type elements. Declare and initialize a variable with 0; it will be used to hold the final integer value. Use the for loop to iterate over the list items. In each iteration: multiply the variable created in the […]

  • Get first n elements of lists in Python
    20 February

    Get First n Elements of List in Python

    Using List slicing Use list slicing to get first n elements of List in Python. Here’s an example: [crayon-65f8f8db48538015931853/] [crayon-65f8f8db4853b520360922/] In the above example, list slicing is used to get a list’s first n elements. As we can see, a list was defined as my_list with 6 elements [10, 20, 30, 40, 50, 60]. And […]

  • 26 December

    Repeat List N Times in Python

    This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and this will repeat list 3 times. See the code below. [crayon-65f8f8db486ba396883996/] Output: [2, 4, 6, 11, […]

  • 16 December

    Convert List to Comma Separated String in Python

    Use .join() Method To convert a list to a comma separated string in Python, use the .join() method. join() method takes all elements in an iterable and joins them into one string with delimiter as separator. [crayon-65f8f8db487a2503836325/] [crayon-65f8f8db487a5840766758/] First, we created a list having string-type elements and named that list list_of_strings. Next, we used the […]

  • 27 November

    Remove Punctuation from List in Python

    Use for loop with replace() Method To remove punctuation from list in Python: Use the for loop to iterate over each word (element) of given list. Use a nested loop to iterate over each character in the word. Use the if statement to check if the current character is in string.punctuation, if it is use […]

  • 23 September

    Add Tuple to List in Python

    Add tuple to list in Python Tuples and Lists are two of the most commonly used collection objects in Python. They both can store multiple elements and provide access to these elements using their indexes. There are a few differences associated with them as well. Tuples are immutable, which means that once created they cannot […]

  • 24 May

    Replace Comma with Space in List in Python

    In this post, we will see how to replace comma with space in the list. How to replace comma with space in list in Python A list is one of the fundamental collection objects in Python. It can store multiple elements and we can access these elements using their respective index. When we display a […]