w3resource

Python: Concatenate elements of a list

Python List: Exercise - 54 with Solution

Write a Python program to concatenate elements of a list.

Python: Concatenate elements of a list

Sample Solution:

Python Code:

# Define a list 'color' containing color names as strings
color = ['red', 'green', 'orange']

# Use the 'join' method to concatenate the elements of the 'color' list with a hyphen '-' in between
# Print the resulting string
print('-'.join(color))

# Use the 'join' method to concatenate the elements of the 'color' list without any separator
# Print the resulting string
print(''.join(color)) 

Sample Output:

red-green-orange   
redgreenorange 

Flowchart:

Flowchart: Concatenate elements of a list

Python Code Editor:

Previous: Write a Python program to create a list with infinite elements.
Next: Write a Python program to remove key values pairs from a list of dictionaries.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.