When I use Console.WriteLine to print a list, it defaults to only showing the first three elements. How do I get it to print the entire contents of the list?
If you just want to know the best way to print a list in Python, here's the short answer: Pass a list as an input to the print() function in Python. Use the asterisk operator * in front of the list to “unpack” the list into the print function. Use the sep argument to define how to separate two list elements visually.
Use list slicing to print specific items in a list, e.g. print(my_list[1:3]) . The print() function will print the slice of the list starting at the specified index and going up to, but not including the stop index.
Print list elements on separate Lines using sep # Use the sep argument to print the elements of a list on separate lines, e.g. print(*my_list, sep='\n') . The items in the list will get unpacked in the call to the print() function and will get printed on separate lines.
You can use the %A format specifier along with printf to get a 'beautified' list printout, but like Console.WriteLine (which calls .ToString()) on the object, it will not necessarily show all the elements. To get them all, iterate over the whole list. The code below shows a few different alternatives.
let smallList = [1; 2; 3; 4]
printfn "%A" smallList // often useful
let bigList = [1..200]
printfn "%A" bigList // pretty, but not all
printfn "Another way"
for x in bigList do
printf "%d " x
printfn ""
printfn "Yet another way"
bigList |> List.iter (printf "%d ")
printfn ""
You can iterate over the it, using the List.iter
function, and print each element:
let list = [1;2;3;4]
list |> List.iter (fun x -> printf "%d " x)
More info:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With