×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python - Create Lists with Mixed Data Types

Last Updated : May 01, 2025

In Python, lists can store elements of different data types, such as integers, strings, and even other lists. In this chapter, we will learn how to create lists with mixed data types, access their elements, and perform operations on them with the help of examples.

Creating a List with Strings and Numbers

Lists can contain elements of different data types. You can create a list that includes both strings and numbers.

Example

In this example, we are creating a list with city names (strings) and population numbers (integers).

# Creating a list with strings and integers
cities_data = ["New York", 8419600, "London", 8982000, "Paris", 2148000]

# Displaying the list
print(cities_data)

The output of the above code would be:

['New York', 8419600, 'London', 8982000, 'Paris', 2148000]

Creating a List with Strings, Floats, and Booleans

Lists can also store elements with different data types such as strings, floats, and booleans in the same list.

Example

In this example, we are creating a list with city names (strings), area sizes (floats), and whether the city is a capital (booleans).

# Creating a list with mixed data types
city_info = ["New York", 789.43, True, "London", 1572.0, True, "Paris", 105.4, True]

# Displaying the list
print(city_info)

The output of the above code would be:

['New York', 789.43, True, 'London', 1572.0, True, 'Paris', 105.4, True]

Creating a List with Nested Lists

A list can also contain other lists as elements. This allows you to create complex data structures where each element is a list itself.

Example

In this example, we are creating a list that contains smaller lists for each city, where each sublist holds the city name, population, and area size.

# Creating a list with nested lists
city_details = [
    ["New York", 8419600, 789.43],
    ["London", 8982000, 1572.0],
    ["Paris", 2148000, 105.4]]

# Displaying the list
print(city_details)

The output of the above code would be:

[['New York', 8419600, 789.43], ['London', 8982000, 1572.0], ['Paris', 2148000, 105.4]]

Creating a List with Mixed Data Types and Accessing Elements

You can access individual elements in a mixed data type list using indexing, just like any other list. If the elements are other lists, you can access their elements using nested indexing.

Example

In this example, we will create a list with mixed data types and then access specific elements from it.

# Creating a list with mixed data types
mixed_list = ["New York", 8419600, 789.43, True, "London", 8982000, 1572.0, False]

# Accessing elements from the mixed list
print(mixed_list[0])   # "New York"
print(mixed_list[1])   # 8419600
print(mixed_list[4])   # "London"
print(mixed_list[7])   # False

The output of the above code would be:

New York
8419600
London
False

Iterating Over a Mixed Data Type List

You can loop through a list containing mixed data types using a for loop. Python will automatically handle each data type during the iteration.

Example

In this example, we will iterate through a mixed data type list and print each element.

# Creating a list with mixed data types
mixed_list = ["New York", 8419600, 789.43, True, "London", 8982000, 1572.0, False]

# Iterating over the list and printing each element
for item in mixed_list:
    print(item)

The output of the above code would be:

New York
8419600
789.43
True
London
8982000
1572.0
False

Exercise

Select the correct option to complete each statement about creating lists with mixed data types in Python.

  1. Which of the following is a valid Python list containing different data types?
  2. What data types can be included in a Python list?
  3. If my_list = [10, 'apple', 3.14, False], what will type(my_list[1]) return?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.