Question

In: Computer Science

Please use Python for both Concatenate List Elements Write a function called concat_list that accepts a...

Please use Python for both

Concatenate List Elements

Write a function called concat_list that accepts a list of strings as an argument and returns a string made up of all elements in the list concatenated together in order. For example, if the argument is ['one', 'two', 'three'], the return value would be 'onetwothree'.

Remove List Duplicates

Write a function called remove_duplicates that accepts a list and returns a list containing the same elements in the same order but with duplicates removed. For example, if the argument is [7, 4, 2, 7, 2, 2, 9, 4], the returned list would be [7, 4, 2, 9].

Solutions

Expert Solution

1).

Code Explanation:

  • pass the list x with strings to the functionconcat_list()
  • In the function initialize an empty string
  • Run a for loop and add i to s which concates the each string in the list
  • After coming out of loop return s
  • Then print the result

Code:

def concat_list(x):
s = ""
for i in x:
s = s+i
return s

x = ['one', 'two', 'three']
res = concat_list(x)
print(res)

O/P:

onetwothree

Code screenshot:

O/P screenshot:

2).

Code Explanation:

  • Pass the list x to the function remove_duplicates()
  • In the function declare an empty list.
  • Iterate through elements in list
  • Use the condition if i not in temp then append the i to temp list
  • After coming out of loop return the temp list
  • Print the result

Code Explanation:

Code:

def remove_duplicates(x):
temp = []
for i in x:
if i not in temp:
temp.append(i)
return temp

x = [7,4,2,7,2,2,9,4]
res = remove_duplicates(x)
print(res)

O/P:

[7, 4, 2, 9]

Code screenshot:

O/P screenshot:

(If you still have any doubts regarding this answer please comment)


Related Solutions

In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input,...
In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input, and returns the highest sum of three neighboring elements in it. Write a main method that initializes the following five lists, gets the ThreeSum result for all of them using the above function, and prints the result to the screen. Example of the output: List 1: [4,5,4,5] , Three sum = 14 List 2: [7] , Three sum = 7 List 3: [ ]...
This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search,...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search, and string to search #for. The function should return everything in the first #string *after* the *second* occurrence of the search term. #You can assume there will always be at least two #occurrences of the search term in the first string. # #For example: # after_second("11223344554321", "3") -> 44554321 # #The search term "3" appears at indices 4 and 5. So, this #returns everything...
Using Python provide the implementation of a function called "isAdjacent" that accepts a string.   The function...
Using Python provide the implementation of a function called "isAdjacent" that accepts a string.   The function checks if two adjacent characters in the string are the same; if they are the same, then return True otherwise return False.   The function should ignore case and check for non-empty string.  If it's an empty string, then return the message 'Empty string'. Sample runs: print( isAdjacent('ApPle')) # True print( isAdjacent('Python')) # False print( isAdjacent('')) # Empty string
Please use the python 3.7 for this assigments Q1 Write a program that accepts the lengths...
Please use the python 3.7 for this assigments Q1 Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is an equilateral triangle. Q2 Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is a right triangle. Recall from the Pythagorean theorem that in a right triangle, the square...
In Python Create a function called ℎ?????. The function has as arguments a list called ??????...
In Python Create a function called ℎ?????. The function has as arguments a list called ?????? and a list call center. • List ?????? contains lists that represent points. o For example, if ?????? = [[4,2], [3,2], [6,1]], the list [4,2] represents the point with coordinate ? at 4 and y coordinate at 2, and so on for the other lists. Assume that all lists within points contain two numbers (that is, they have x, y coordinates). • List ??????...
(IN PYTHON) Write a function that accepts a line of text and a single letter as...
(IN PYTHON) Write a function that accepts a line of text and a single letter as input (case insensitive) and returns the number of times the letter is the last character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter.
In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
Write a python function to fulfill the requirements. The function accepts a string, a current state,...
Write a python function to fulfill the requirements. The function accepts a string, a current state, edges’ information, and an accepting state. The output of the function is a boolean value verifying if the string can pass the finite state machine or not.             ### Finite State Machine Simulator in Python ### Provide s1 and s2 that are both accepted, but s1 != s2. s1 = "bdf" s2 = "bdgbdf" edges = {(1,'a') : 2,                (1,'b') : 3,       ...
use python to solve Write a function called word_chain that repeatedly asks the user for a...
use python to solve Write a function called word_chain that repeatedly asks the user for a word until either (1) the user has entered ten words, or (2) the user types nothing and presses enter. Each time the user enters an actual word, your program should print all the words entered so far, in one long chain. For example, if the user just typed "orange", and the user has already entered "apple" and "banana", your program should print "applebananaorange" before...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT