'Creating a data structure with unknown size python

I want to create a data structure (not sure if it should be a list or a dictionary) but I don't know it's size in advance. How can I proceed to create this structure and add an unknown ammount of elements to it by input?



Solution 1:[1]

You can use a list in python, you can use the list.append(item) method in order to insert new elements into it, you don't have to specify the size of the list in order to use it and it will grow as your append more items.

Solution 2:[2]

If you just want a while loop to stop after some input. You can try this. It will break at enter key.

list = []
while True:
    x = input("Enter numbers:")
    if x =='':
        break
    else:
        list.append(int(x))
print(list)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Idanushka
Solution 2 abhishek