How to get string values from list in Python

In this Python tutorial, we will discuss different ways to get string values from a list in Python.

A Python list is a collection of items that can be of different types. On the other hand, a string is a sequence of characters. Let’s discuss how we will explore various ways to extract a string from a list in Python.

Python Extracting Individual Strings from a List

If you have a Python list and you want to extract a specific string, you can do so by referencing its index. Python’s list indices start from 0.

states = ['Florida', 'Ohio', 'Virginia', 'Michigan']
print(states[1])

In this example, we have a Python list of U.S. states and we want to extract a specific state, so we do so by referencing its index. We extract the state at index 1, which is ‘Ohio’.

Output:

How to get string values from list in Python

Extracting Substrings from a List of Strings in Python

Suppose you have a Python list and you want to extract only the Python string. Here is how you can use list comprehension to achieve this:

cities = ['New York_8336817', 'Los Angeles_3979576', 'Chicago_2693976']
city_names = [s.split('_')[0] for s in cities]
print(city_names) 
  • In this example, we use a Python list comprehension to create a new Python list, city_names, where each element is a Python substring of the corresponding element in the original Python list cities.
  • The split('_')[0] operation splits each Python string at the underscore and takes the first part.

Output:

How to get string values from list in Python Example

Python Searching for a String in a List

If you want to check whether a certain Python string is in a Python list, you can use the in keyword.

states = ['Texas', 'California', 'Florida', 'New York']
search = 'Florida'
if search in states:
    print(f"{search} is in the list")
else:
    print(f"{search} is not in the list")

This script prints “Florida is in the list” because ‘Florida’ does indeed exist in the list.

Output:

Get string values from list in Python

Using Python Map Function

The Python map function is a standard function that accepts two arguments, a function and an iterable, and returns a map object that applies the function to every item of the iterable. This can be used to extract or manipulate strings in a Python list.

populations = ['328239523', '331449281', '1459120256']
populations = list(map(int, populations)) 
print(populations)  

Here, we’ve taken a Python list of populations of U.S., India, and China (as strings), and converted them to integers using map().

Output:

Get string values from list in Python Example

Using a For Loop in Python

A for loop can be used to iterate through a Python list and perform actions on each item. This is a more manual approach compared to using methods like join() or map(), but it provides the most flexibility.

car_brands = ['Ford', 'Chevrolet', 'Dodge']
result = ''
for brand in car_brands:
    result += brand + ' '
print(result.strip()) 

This code takes each brand from the Python list and appends it to the result Python string with a space in between.

Output:

How to get string values from list using Python

Using Python List Indexing

Python allows negative indexing, which can be used to extract strings from the end of the Python list.

states = ['California', 'Texas', 'Florida', 'New York', 'Ohio']
print(states[-1]) 

Here, we print the last state from the Python list, Ohio, using negative indexing.

Output:

How to get string values from list using Python Example

Read: How to compare two lists in Python

Using the Filter Function in Python

The Python filter function constructs an iterator from elements of an iterable for which a function returns true. This is useful when you want to extract certain elements from a Python list based on some condition.

states = ['California', 'Texas', 'Florida', 'New York', 'Washington']
long_names = list(filter(lambda state: len(state) > 7, states))
print(long_names) 

In this case, we’ve filtered for states with names longer than 7 characters.

Output:

Get string values from list using Python

Conclusion

In summary, Python provides several methods to effectively extract strings from a list. Depending on your use case, you may use the join function, refer to a specific index, use list comprehension to extract substrings, etc.

Related Python tutorials: