Can a list contain items of different types?


#1

Question

Is it possible to put different types of data in the same list variable?

Answer

A list in Python CAN contain different types of data. Each item in the list is separated by a comma and the entire list is enclosed in square brackets []. To add a new item to the list, just edit the list starting before the closing bracket. Add a comma and the new item. It doesn’t matter what the type is for the previous item.

In the example below, the elements list contains the name of an element and its atomic number and mass. The list then contains the same data for another element. The data types contained in the list are strings, integers, and floating point values

elements = [‘Hydrogen’, 1, 1.007825, ‘Helium’, 2, 4.00260]